Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nancyhabecker/29d7daad9557c8c6622924e389ba36ba to your computer and use it in GitHub Desktop.
Save nancyhabecker/29d7daad9557c8c6622924e389ba36ba to your computer and use it in GitHub Desktop.
bluebird each vs map vs map (with concurrency) vs mapSeries
const bluebird = require('bluebird');
function mapItem(item) {
console.log('mapItem called for ' + item);
return bluebird.delay(1000).then(() => {
console.log('returning for ' + item);
return item * 10;
});
}
// bluebird.each prints the following:
// > mapItem called for 1
// > returning for 1
// > mapItem called for 2
// > returning for 2
// > mapItem called for 3
// > returning for 3
// > mapItem called for 4
// > returning for 4
// > [ 1, 2, 3, 4 ]
bluebird.each([1,2,3,4], mapItem).then(console.log);
// bluebird.map prints the following:
// > mapItem called for 1
// > mapItem called for 2
// > mapItem called for 3
// > mapItem called for 4
// > returning for 1
// > returning for 2
// > returning for 3
// > returning for 4
// > [ 10, 20, 30, 40 ]
bluebird.map([1,2,3,4], mapItem).then(console.log);
// bluebird.map with concurrency prints the following:
// > mapItem called for 1
// > returning for 1
// > mapItem called for 4
// > returning for 4
// > mapItem called for 3
// > returning for 3
// > mapItem called for 2
// > returning for 2
// > [ 10, 20, 30, 40 ]
bluebird.map([1,2,3,4], mapItem, {concurrency: 1}).then(console.log);
// bluebird.mapSeries prints the following:
// > mapItem called for 1
// > returning for 1
// > mapItem called for 2
// > returning for 2
// > mapItem called for 3
// > returning for 3
// > mapItem called for 4
// > returning for 4
// > [ 10, 20, 30, 40 ]
bluebird.mapSeries([1,2,3,4], mapItem).then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment