ember-cli-sync-for-each
v1.0.8
Published
Synchronously calls a function on each item of an array. If that function is asynchronous, it will wait for the previous call to resolve before triggering the next.
Downloads
3
Maintainers
Readme
Ember-cli-sync-for-each
This addon provides you with a function to execute asynchronous callbacks synchronously on an array of objects. This is for example usefull if you want to save a list of models where each model depends on data that is returned by the backend for one of the previous models (such as the id). Or when you want to mass create a list of objects but also respect their order of creation for the timestamps generated by your backend. For more use cases see the guide section below.
API
syncForEach(array, callback, force, index);
| Parameter | Description |
| ------------- |:-----|
| array | List of items for which the callback is going to be executed. |
| callback | Function that is going to be executed synchronously for every item. |
| | signature: (item, index, array) |
| force | If set to true
, execution will not stop should the callback reject for one item. |
| index | Specifies at what position the function should start traversing the array. (used internally) |
Guide
simple usage
When your user is able to mass create a list of todos and you retroactively want to commit them to your backend, varying network roundtrip times can mess with their order of creation. For most use cases this is fine, but sometimes your application depends on respecting that order. You can use syncForEach
to throttle the POST
requests in a way that every request will only be sent once the previous request returned successfully.
import syncForEach from 'ember-cli-sync-for-each';
syncForEach(model.get('todos'), function(todo) {
return todo.save();
});
access enumerable
The callback additionally gives you access to the enumerable. You can use syncForEach
therefor, to access resolved data from previous calls. In the following example we want to commit a list of new employees to our company. There is a policy that every new employee will be mentored by the last hired coworker. Since syncForEach
will already have successfully saved all previous coworkers they will already have ids set by the backend that you can reference.
syncForEach(employees, function(employee, index, employees) {
previousEmployee = index === 0 ? null : employees[index-1];
employee.set('mentor', previousEmployee);
return employee.save();
});
resolves upon completion
Since syncForEach
will either resolve once it executed the callback for every item or rejects as soon as its callback rejects for one of the items, you can use it to asynchronously run code in either event.
syncForEach(model.get('todos'), function(todo) {
return todo.save();
}).then(function(todos){
alert('The backend created todos with the following ids: ', todos.mapBy('id'));
});
optionally resumes on rejection
syncForEach
s default behaviour is to stop execution once one callback rejects, so the following code:
syncForEach([1,2,3,4,5], function(item) {
return new Ember.RSVP.Promise(function(resolve, reject){
if (item === 3) {
reject();
} else {
console.log(item);
resolve();
}
});
});
will produce this output:
> 1
> 2
If you want the function to continue no matter what, you can set the optionally force
flag to true
. The promise that will be returned from syncForEach
will still reject with the respective rejection message but your callback will be executed for the remaining items anyway.
promise = syncForEach([1,2,3,4,5], function(item) {
return new Ember.RSVP.Promise(function(resolve, reject){
if (item === 3) {
reject();
} else {
console.log(item);
resolve();
}
});
}, true);
promse.then(function() {
console.log('yay =D');
}, function() {
console.log('nay D=');
});
will produce the following output:
> 1
> 2
> 4
> 5
> nay D=
synchronous callback
syncForEach
is smart enough to recognize wether your callback actually returns a promise and will behave like a normal forEach
in case it does not. The function will still return a resolved promise in this case. So the following snippet:
syncForEach(['foo', 'bar', 'baz'], function(word) {
console.log(word);
});
will output:
> foo
> bar
> baz
indicate loading
Imagine you need to load a bunch of different models with so much data, that you want to display some kind of loading indicator. With syncForEach
you can display a loading message to the user while loading your model data one after another, allways updating the message after each successfull load.
_this = this;
syncForEach(['account', 'user', 'report', 'statistic'], function(model) {
$('#loading-indicator').html('loading ' + model + 's');
_this.store.find(model);
});
Installation
To use this addon in your project, just type:
$ ember install:addon ember-cli-sync-for-each
or for older versions of ember-cli (pre 1.4.0):
$ npm install --save-dev ember-cli-sync-for-each
and then import the function wherever you need it:
import syncForEach from 'ember-cli-sync-for-each';
Contributing
Im happy about everyone that wants to contribute, even opening an issue on github. However if you want to contribute to the code just follow the setup instructions below. If you are scared, since you've never contributed to open source projects before, you are in luck! This is a pretty simple addon. Just have a look into the implementation, and unit tests, that's all there is to it.
git clone https://github.com/lazybensch/ember-cli-sync-for-each
cd ember-cli-sync-for-each
npm install
bower install
ember test