console.log(1)
setTimeout(()=>console.log(2),0)
Promise.resolve(3).then(console.log)
console.log(4)
consider the above code. Here is what I expect from the code.
- 1 is logged out, setTimeout is an async function so it gets added to the queue.
- The promise is resolved inline but after that, whatever is in
then
acts as async. so it also gets added to the queue. - Then 4 is logged out and there is no more inline code left.
- So, setTimeout will get executed from queue printing 2 and after that 3.
- therefore the final output should be 1 4 2 3
But the output comes to be 1 4 3 2. can you please explain to me what is actually happening here.