Last active
August 29, 2015 14:08
-
-
Save clarle/e35beac57d1dda98cda4 to your computer and use it in GitHub Desktop.
Promise-based operations in sequence, with Bluebird
This file contains 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
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