Last active
October 4, 2021 13:17
-
-
Save motss/93608f29ade7122258c4fc35e75f5a9c to your computer and use it in GitHub Desktop.
Crack the event loop
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 main() { | |
console.log('a'); | |
const a = new Promise((resolve) => { | |
resolve('b'); | |
console.log('c'); | |
}).then(console.log); | |
setTimeout(async () => { | |
b(); | |
console.log('d'); | |
}, 0); | |
async function b() { | |
console.log('e'); | |
} | |
console.log('f'); | |
a.then(() => console.log('g')); | |
} | |
main(); |
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
{ | |
debugger; | |
function main() { | |
console.log('1'); | |
setTimeout(() => new Promise((y, n) => { | |
y('9'); | |
}).then(console.log), 0); | |
setTimeout(async () => { | |
const d = await Promise.resolve('8'); | |
console.log(d); | |
}, 0); | |
setTimeout(() => { a = Promise.resolve('7'); }, 0); | |
Promise.resolve('6').then(console.log); | |
setTimeout(() => console.log('2'), 0); | |
setTimeout(() => Promise.resolve('4').then(console.log).then(() => console.log('5')), 0); | |
a && a.then(console.log); | |
console.log('3'); | |
} | |
main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
debugger
setTimeout(function(){console.log(4)},0);
new Promise(function(resolve){
console.log(1);
for ( var i=0 ; i<10000 ; i++ ){ i==9999 && resolve() }
console.log(2)
}).then(function(){ console.log(5) });
console.log(3);