Created
June 4, 2024 03:32
-
-
Save tinvaan/822f6ba2ece54d95e179d90b0223863e to your computer and use it in GitHub Desktop.
await <Promise> vs Promise.all()
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
async function delay(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)) | |
} | |
const serial = async () => { | |
console.log("Serial sleep!") | |
await delay(5000) | |
await delay(5000) | |
} | |
const parallel = async () => { | |
console.log("Parallel sleep!!") | |
await Promise.all([delay(5000), delay(5000)]) | |
} | |
if (require.main === module) { | |
if (process.argv[2] === "0") { serial(); return } | |
if (process.argv[2] === "1") { parallel(); return } | |
} |
Author
tinvaan
commented
Jun 4, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment