Skip to content

Instantly share code, notes, and snippets.

@NelsonBrandao
Created March 28, 2016 11:04
Show Gist options
  • Select an option

  • Save NelsonBrandao/5bc4771d16769ba22482 to your computer and use it in GitHub Desktop.

Select an option

Save NelsonBrandao/5bc4771d16769ba22482 to your computer and use it in GitHub Desktop.
Promise Collections
var q = require('q');
// Parallel
q.all(array.map(function (elem) {
return processElem(elem);
}));
// Or simply
q.all(array.map(processElem));
var q = require('q');
// If the current element depends on the ones below
array.reduce(function (promise, elem) {
return promise.then(function (result) {
return processElem(elem, result);
});
}, q());
// If not
array.reduce(function (promise, elem) {
return promise.then(function () {
return processElem(elem);
});
}, q());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment