Last active
August 28, 2020 03:05
-
-
Save anhnd3/407d3e46f902c8105ca65fcee70d781b to your computer and use it in GitHub Desktop.
Test event loop in javascript
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
function foo() { | |
console.log('foo'); | |
} | |
function bar() { | |
setTimeout(foo); | |
console.log('bar'); | |
} | |
function baz() { | |
setTimeout(() => console.log('baz')); | |
} | |
function liz() { | |
requestAnimationFrame(() => console.log('liz')); | |
} | |
function rep() { | |
Promise.resolve().then(() => console.log('rep')); | |
} | |
function rep2() { | |
setTimeout(()=>Promise.resolve().then(() => console.log('rep2'))); | |
} | |
console.log('start'); | |
rep2(); | |
foo(); | |
bar(); | |
baz(); | |
liz(); | |
rep(); | |
console.log('end'); | |
/* | |
Result | |
1. start, foo, bar, end, rep, rep2, foo, baz, liz | |
2. start, foo, foo, bar, baz, liz, rep, rep2 end | |
3. start, end, foo, bar, rep2, foo, baz, liz, rep | |
4. start, foo, bar, end, foo, baz, liz, rep, rep2 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment