-
-
Save midnightcodr/9d1b6d919d7e149acec95e15ceb53d00 to your computer and use it in GitHub Desktop.
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
// 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 |
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
// 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