Skip to content

Instantly share code, notes, and snippets.

@carefree-ladka
Created August 23, 2024 13:56
Show Gist options
  • Select an option

  • Save carefree-ladka/1a6eb9a803721db192f096dc68488691 to your computer and use it in GitHub Desktop.

Select an option

Save carefree-ladka/1a6eb9a803721db192f096dc68488691 to your computer and use it in GitHub Desktop.
Tricky Event Loop Based Question
console.log('Start');
setTimeout(() => {
console.log('Inside setTimeout');
}, 0);
Promise.resolve()
.then(() => {
console.log('Inside first promise');
return new Promise((resolve) => {
setTimeout(() => {
console.log('Inside promise-setTimeout');
resolve();
}, 0);
});
})
.then(() => {
console.log('Inside second promise');
});
setTimeout(() => {
console.log('Inside second setTimeout');
Promise.resolve().then(() => {
console.log('Inside promise within setTimeout');
});
}, 0);
console.log('End');
/*
Start
End
Inside first promise
Inside setTimeout
Inside second setTimeout
Inside promise within setTimeout
Inside promise-setTimeout
Inside second promise
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment