Skip to content

Instantly share code, notes, and snippets.

@stephantabor
Last active January 6, 2024 04:18
Show Gist options
  • Save stephantabor/833a8cd26fc37b420c75 to your computer and use it in GitHub Desktop.
Save stephantabor/833a8cd26fc37b420c75 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);
});
};
}
@JonahTurnquist
Copy link

Great example. Very insightful.

@mrsum
Copy link

mrsum commented Feb 14, 2017

Yo, can I in current promise get data which was return in previous Promise?

@the-vampiire
Copy link

Thank you so much for this example

@Shwetabh1
Copy link

Really Helpful. Thanks a ton!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment