Skip to content

Instantly share code, notes, and snippets.

@motss
Last active October 4, 2021 13:17
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 motss/93608f29ade7122258c4fc35e75f5a9c to your computer and use it in GitHub Desktop.
Save motss/93608f29ade7122258c4fc35e75f5a9c to your computer and use it in GitHub Desktop.
Crack the event loop
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();
{
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();
}
@motss
Copy link
Author

motss commented Nov 16, 2018

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment