Skip to content

Instantly share code, notes, and snippets.

@Loupax
Created November 6, 2015 14:15
Show Gist options
  • Select an option

  • Save Loupax/67d09d1aeb69c876cf43 to your computer and use it in GitHub Desktop.

Select an option

Save Loupax/67d09d1aeb69c876cf43 to your computer and use it in GitHub Desktop.
/**
* Runs an array of promises one after another instead of in parallel
*
* @param array
* @param callback
*
* @returns {*}
*/
function sequential(array, callback) {
var result = [];
return Q(array.reduce(function (previousPromise, item) {
return previousPromise.then(function () {
return callback(item);
}).then(function (res) {
result.push(res);
return res;
});
}, Q([]))).then(function () {
return result;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment