Skip to content

Instantly share code, notes, and snippets.

@rafaeljesus
Forked from stephantabor/bb.js
Created December 4, 2015 13:28
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rafaeljesus/ce47e897ed6f164cffdd to your computer and use it in GitHub Desktop.
Save rafaeljesus/ce47e897ed6f164cffdd to your computer and use it in GitHub Desktop.
Bluebird .each vs .mapSeries vs .map
var Promise = require('bluebird');
var funcs = Promise.resolve([500, 100, 400, 200].map((n) => makeWait(n)));
funcs
.each(iterator) // logs: 500, 100, 400, 200
.then(console.log) // logs: [ [Function], [Function], [Function], [Function] ]
funcs
.mapSeries(iterator) // logs: 500, 100, 400, 200
.then(console.log) // logs: [500, 100, 400, 200]
funcs
.map(iterator) // logs: 100, 200, 400, 500
.then(console.log) // logs: [500, 100, 400, 200]
function iterator(f) {
return f()
}
function makeWait(time) {
return function () {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(time);
resolve(time);
}, time);
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment