Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created April 1, 2021 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/ff2b1a67ef7e3cff46b32373f9c1f90e to your computer and use it in GitHub Desktop.
Save JoshCheek/ff2b1a67ef7e3cff46b32373f9c1f90e to your computer and use it in GitHub Desktop.
JS syncing promises code sample
# 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