Skip to content

Instantly share code, notes, and snippets.

@shqld
Last active April 13, 2021 14:10
Show Gist options
  • Save shqld/8ea2ac6f4e1342fca8620a96c0f737c3 to your computer and use it in GitHub Desktop.
Save shqld/8ea2ac6f4e1342fca8620a96c0f737c3 to your computer and use it in GitHub Desktop.
Event Loop Race
race()
function race() {
let count = 1
function finish(message) {
console.log(`${count++}. ${message}`)
}
setTimeout(() => finish('timeout'), 0)
if (typeof postMessage !== 'undefined') {
addEventListener(
'message',
({ data }) => data === 'race' && finish('onmessage'),
{ once: true }
)
postMessage('race')
}
if (typeof setImmediate !== 'undefined')
setImmediate(() => finish('immediate'))
if (typeof requestAnimationFrame !== 'undefined')
requestAnimationFrame(() => finish('raf'))
if (typeof process !== 'undefined')
process.nextTick(() => finish('nextTick'))
console.log('race has started')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment