Skip to content

Instantly share code, notes, and snippets.

@alexlafroscia
Created October 14, 2016 23:34
Show Gist options
  • Save alexlafroscia/9515239774482095d7bfe3afd170ce15 to your computer and use it in GitHub Desktop.
Save alexlafroscia/9515239774482095d7bfe3afd170ce15 to your computer and use it in GitHub Desktop.
Resolve an array of promises, one after the other
/**
* For each item in the array, call the callback, passing the item as the argument.
* However, only call the next callback after the promise returned by the previous
* one has resolved.
*
* @param {Array<*>} items the elements to iterate over
* @param {Function} cb called for each item, with the item as the parameter. Should return a promise
* @return {Promise}
*/
function synchronize(items, cb) {
return items.reduce(function(promise, item) {
return promise.then(function() {
return cb.call(this, item);
});
}, RSVP.resolve());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment