Skip to content

Instantly share code, notes, and snippets.

@clarle
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarle/e35beac57d1dda98cda4 to your computer and use it in GitHub Desktop.
Save clarle/e35beac57d1dda98cda4 to your computer and use it in GitHub Desktop.
Promise-based operations in sequence, with Bluebird
var Promise = require('bluebird'),
data = ['a', 'b', 'c', 'd', null, 'e'];
/**
* A long running task to run for each element of the array.
* It must return a promise
*/
function doThing(arg) {
return new Promise(
function (resolve, reject) {
setTimeout(function () {
console.log(arg);
if (arg) {
resolve('resolved - ' + arg);
} else {
reject('Error: falsy value');
}
}, 1000);
}
);
}
console.log('and we\'re off!');
Promise.each(data, doThing);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment