Created
March 23, 2019 21:59
-
-
Save micahriggan/975fd3752c56d2ba156de9d654b6d679 to your computer and use it in GitHub Desktop.
Serial Promises vs Parallel Promises
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
function wait(waitTime) { | |
return new Promise(resolve => setTimeout(() => { | |
console.log(`waited ${waitTime} ms`) | |
resolve() | |
}, waitTime)); | |
} | |
async function serial() { | |
console.time('serial'); | |
await wait(1000); | |
await wait(1000); | |
await wait(1000); | |
console.timeEnd('serial'); | |
} | |
async function parallel() { | |
console.time('parallel'); | |
await Promise.all([ | |
wait(1000), | |
wait(1000), | |
wait(1000) | |
]) | |
console.timeEnd('parallel'); | |
} | |
async function test() { | |
await serial(); | |
await parallel(); | |
} | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment