Skip to content

Instantly share code, notes, and snippets.

@tinvaan
Created June 4, 2024 03:32
Show Gist options
  • Save tinvaan/822f6ba2ece54d95e179d90b0223863e to your computer and use it in GitHub Desktop.
Save tinvaan/822f6ba2ece54d95e179d90b0223863e to your computer and use it in GitHub Desktop.
await <Promise> vs Promise.all()
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 }
}
@tinvaan
Copy link
Author

tinvaan commented Jun 4, 2024

time node ~/test.ts 0
Serial sleep!
node ~/test.ts 0  0.03s user 0.01s system 0% cpu 10.041 total

➞  time node ~/test.ts 1
Parallel sleep!!
node ~/test.ts 1  0.03s user 0.01s system 0% cpu 5.040 total

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment