-
-
Save hvrauhal/3fd1367393e4cd6a34602cb800c63e7a to your computer and use it in GitHub Desktop.
Bluebird .mapSeries vs .map
This file contains hidden or 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'); | |
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 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PERFECT EXAMPLE ❤️ 💖