Created
October 14, 2016 23:34
-
-
Save alexlafroscia/9515239774482095d7bfe3afd170ce15 to your computer and use it in GitHub Desktop.
Resolve an array of promises, one after the other
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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