Skip to content

Instantly share code, notes, and snippets.

@micahriggan
Created March 23, 2019 21:59
Show Gist options
  • Save micahriggan/975fd3752c56d2ba156de9d654b6d679 to your computer and use it in GitHub Desktop.
Save micahriggan/975fd3752c56d2ba156de9d654b6d679 to your computer and use it in GitHub Desktop.
Serial Promises vs Parallel Promises
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