Last active
October 10, 2017 19:33
-
-
Save hackjutsu/3b8523cc3dcfc46c7022a06ea22f8cd3 to your computer and use it in GitHub Desktop.
[Serialize bluebird promise] each() vs mapSeries() vs map() #promise #bluebird
This file contains 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([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