Reputation: 159
First of all, the docs have clearly stated that in a I/O cycle, setImmediate() will always run before setTimeout(). What is bugging me is that they didn't explain why it functions like that, and looking for clues in the previous statements in their docs somehow pointed out to me the opposite (that setTimeout() should be called first). Here's why:
When the callback finishes, there are no more callbacks in the queue, so the event loop will see that the threshold of the soonest timer has been reached then wrap back to the timers phase to execute the timer's callback
and
Once the poll queue is empty the event loop will check for timers whose time thresholds have been reached. If one or more timers are ready, the event loop will wrap back to the timers phase to execute those timers' callbacks.
So it seems like the first thing the event loop prioritizes to do after exhaust the poll queue is to check the timers and wraps back there if they are timeouted. So in that regard it should be the setTimeout() be executed first.
I'm no advance programmer who can read source code in github to see how the libuv library works internally. Really appreciate some help from you guys.
Upvotes: 3
Views: 730
Reputation: 754
You can't guarantee the setTimeout or setInterval callbacks execute first because timers has duration by default 1 miliseconds even you put 0 in 2nd argument, so what happen is the timer phase passed through and goes to next phase (assuming the next is check phase) then it shows to your console log the setImmediate callback looks executed first. the event loop needs to reiterate in order to execute the pending callbacks.
You must understand how event loop works first, then understand the concept in order for you not to confused in event loop phases.
Upvotes: 0
Reputation: 1403
The event loop moves only clockwise, it does not move backwards. So in order to go to the timers phase, the event loop has has to go through the check phase which executed the setImmediate callbacks. That is why in a I/o cycle setImmediate will be called first whereas otherwise setTimeout will be called first.
Upvotes: 1