Skip to content

Instantly share code, notes, and snippets.

@anhnd3
Last active August 28, 2020 03:05
Show Gist options
  • Save anhnd3/407d3e46f902c8105ca65fcee70d781b to your computer and use it in GitHub Desktop.
Save anhnd3/407d3e46f902c8105ca65fcee70d781b to your computer and use it in GitHub Desktop.
Test event loop in javascript
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