Created
April 1, 2021 23:39
-
-
Save JoshCheek/ff2b1a67ef7e3cff46b32373f9c1f90e to your computer and use it in GitHub Desktop.
JS syncing promises code sample
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
# https://twitter.com/josh_cheek/status/1377767258601181189 | |
node -e ' | |
const sleep = n => new Promise(resolve => setTimeout(resolve, n)) | |
!async function() { | |
const start = Date.now() | |
const val1 = sleep(1000).then(() => console.log({ line: 5, at: Date.now()-start })) | |
const val2 = sleep(1500).then(() => console.log({ line: 6, at: Date.now()-start })) | |
const val3 = sleep(500 ).then(() => console.log({ line: 7, at: Date.now()-start })) | |
await Promise.all([val1, val2, val3]) // <-- IS THERE ANYTHING BETTER THAN THIS YET?!?!!?!? | |
console.log({ line: 9, at: Date.now()-start }) | |
}()' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment