Created
May 9, 2022 07:50
-
-
Save SamWSoftware/b9dc34ecd56300b41b8c25ca408b6079 to your computer and use it in GitHub Desktop.
forOf vs map with promise array
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
const testForOfAsync = async () => { | |
const start = Date.now(); | |
const getFruits = [ | |
new Promise((resolve) => {setTimeout(() => resolve('apple'), 10000)}), | |
new Promise((resolve) => {setTimeout(() => resolve('banana'), 100)}), | |
new Promise((resolve) => {setTimeout(() => resolve('orange'), 100)}) | |
] | |
for await (const fruit of getFruits) { | |
console.log(fruit, Date.now() - start) | |
} | |
} |
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
const testMapAsync = async () => { | |
const start = Date.now(); | |
const getFruits = [ | |
new Promise((resolve) => {setTimeout(() => resolve('apple'), 10000)}), | |
new Promise((resolve) => {setTimeout(() => resolve('banana'), 100)}), | |
new Promise((resolve) => {setTimeout(() => resolve('orange'), 100)}) | |
] | |
await Promise.all( | |
getFruits.map(async (fruitPromise) => { | |
const fruit = await fruitPromise; | |
console.log(fruit, Date.now() - start) | |
}) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment