Have you ever wondered why some pieces of JavaScript code seem to run out of order? The key to understanding this is the event loop.
JavaScript's ...
For further actions, you may consider blocking this person and/or reporting abuse
Great informationπ€π»π€π». Little suggestion for your upcoming article, Use images or gifs. That takes your article on top. Waiting for your next article..
Thanks for the suggestion, Krunal
Such articles worth reading
Thank you, Chandhar
amazing article thank you
Thank you, RogeClash
Great and straight to the point. Loved reading it. Kudos
Thank you for your feedback, Daniel π
Why in the last examples, the fallback setTimeOut executed first?
The reason the "fallback setTimeout" (i.e.,
Step 6: Immediate (using setTimeout with 0 delay as fallback)
) is executed beforeStep 5: Timeout 2
has to do with when each setTimeout is added to the macrotask queue (scheduled).Since
Step 5: Timeout 2
was inside a setTimeout (a macrotask) and was called within a promise (a microtask), it experienced a delay before being scheduled. Meanwhile,Step 6: Immediate (using setTimeout with 0 delay as fallback)
was called outside of a promise and was scheduled immediately.What this means is that the second setTimeout (Step 5) was delayed, while the first setTimeout (Step 6) was processed right away.
I hope this explanation clarifies things better!
For Macro tasks, in the definition, it's mentioned that they are executed before next event loop but in the comparison table it's mentioned executed in the next event loop! Which one is correct?