Google's Jake Archibald make excellent points in the Async functions document about not getting too sequential with your awaits. The idea is to avoid stacking awaits, when possible, and instead trigger tasks immediately and use await after said tasks are triggered:
Created
February 8, 2018 00:23
-
-
Save etoxin/ea3f2fd8fa35f31819195950fd998ec8 to your computer and use it in GitHub Desktop.
Parallelism
This file contains hidden or 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
// Will take 1000ms total! | |
async function series() { | |
await wait(500); | |
await wait(500); | |
return "done!"; | |
} | |
// Would take only 500ms total! | |
async function parallel() { | |
const wait1 = wait(500); | |
const wait2 = wait(500); | |
await wait1; | |
await wait2; | |
return "done!"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment