Skip to content

Instantly share code, notes, and snippets.

@hvrauhal
Forked from stephantabor/bb.js
Last active June 3, 2019 10:59
Show Gist options
  • Save hvrauhal/3fd1367393e4cd6a34602cb800c63e7a to your computer and use it in GitHub Desktop.
Save hvrauhal/3fd1367393e4cd6a34602cb800c63e7a to your computer and use it in GitHub Desktop.
Bluebird .mapSeries vs .map
var Promise = require('bluebird');
var funcs = Promise.resolve([100, 200, 300, 400].map((n) => makeWait(n)));
console.log('first with {concurrency: 1}');
funcs
.map(iterator, {concurrency: 1})
.then(function(r) { console.log('Resolved with', r) } )
.then(function() {
console.log('then with mapSeries');
funcs
.mapSeries(iterator)
.then(function(r) { console.log('Resolved with', r) } )
})
function iterator(f) {
return f()
}
function makeWait(time) {
return function () {
return new Promise((resolve, reject) => {
console.log('Starting to wait for', time);
setTimeout(() => {
console.log('Waited for', time);
resolve(time);
}, time);
});
};
}
// OUTPUT:
// first with {concurrency: 1}
// Starting to wait for 100
// Waited for 100
// Starting to wait for 400
// Waited for 400
// Starting to wait for 300
// Waited for 300
// Starting to wait for 200
// Waited for 200
// Resolved with [ 100, 200, 300, 400 ]
// then with mapSeries
// Starting to wait for 100
// Waited for 100
// Starting to wait for 200
// Waited for 200
// Starting to wait for 300
// Waited for 300
// Starting to wait for 400
// Waited for 400
// Resolved with [ 100, 200, 300, 400 ]
@samarthsikotara
Copy link

samarthsikotara commented Jun 3, 2019

PERFECT EXAMPLE ❤️ 💖

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