Skip to content

Instantly share code, notes, and snippets.

@slmyers
Last active May 8, 2020 14:58
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 slmyers/fdf5a278dfadeb86011b4bf3a60f2b41 to your computer and use it in GitHub Desktop.
Save slmyers/fdf5a278dfadeb86011b4bf3a60f2b41 to your computer and use it in GitHub Desktop.
exchange js gists 5/7/2020, for a game
// adds a function to the microtask queue, will not print anything this tick
Promise.resolve().then(() => console.log(2))
// this will execute on the current phase of the event loop
console.log(1)
// synchronous execution
console.log(1)
// ?? I would expect this last
await Promise.reject().catch(() => console.log(2))
// ?? I would expect this third
Promise.resolve().then(() => console.log(4))
// new Promise is synchronous, but I would expect this before `2`
new Promise((resolve, reject) => {
resolve()
console.log(3)
})
// new Promise is synchronous await doesn't impede execution?
await new Promise((res, rej) => {
console.log(1)
res()
console.log(2)
})
// ?? I would expect this to be 4
await Promise.resolve().then(() => console.log(3))
// ?? I thought this would be 3
await (console.log(4))
// 1 and 2 are executed in the order they're encountered
await new Promise((res, rej) => rej()).catch(() => console.log(1))
try {
await Promise.reject()
} catch (e) {
console.log(2)
}
// await blocks execution so this happens last
console.log(3)
// sync
new Promise((res, rej) => rej()).catch(() => console.log(1))
try {
// Uncaught (in promise)
Promise.reject()
} catch (e) {
console.log("NEVER")
}
// sync
console.log(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment