Skip to content

Instantly share code, notes, and snippets.

@midnightcodr
Created November 7, 2017 16:54
Show Gist options
  • Save midnightcodr/9d1b6d919d7e149acec95e15ceb53d00 to your computer and use it in GitHub Desktop.
Save midnightcodr/9d1b6d919d7e149acec95e15ceb53d00 to your computer and use it in GitHub Desktop.
// using array
const bluebird = require('bluebird')
const things = [{a: 1}, {b: 2}, {c: 3}]
function doit(doc) {
return bluebird.delay(1000).then(() => {return doc})
}
async function main() {
console.time('abc')
const results = [], len = things.length
for(var i=0;i<3;i++) {
results.push(await doit(things[i]))
}
console.log(results)
console.timeEnd('abc')
}
main()
// [ { a: 1 }, { b: 2 }, { c: 3 } ]
// abc: 3015.500ms
// using Promise.all
var bluebird=require('bluebird')
var things = [{a: 1}, {b: 2}, {c: 3}]
function doit(doc) {
return bluebird.delay(1000).then(() => {return doc})
}
async function main() {
console.time('abc')
const results = await Promise.all(things.map(doit))
console.log(results)
console.timeEnd('abc')
}
main()
// [ { a: 1 }, { b: 2 }, { c: 3 } ]
// abc: 1007.932ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment