15

I was going through node docs for event loop and I got very confused. It says -

timers: this phase executes callbacks scheduled by setTimeout() and 
setInterval().
I/O callbacks: executes almost all callbacks with the exception of close callbacks, the ones scheduled by timers, and setImmediate().
idle, prepare: only used internally.
poll: retrieve new I/O events; node will block here when appropriate.
check: setImmediate() callbacks are invoked here.
close callbacks: e.g. socket.on('close', ...).

Then in detailed poll phase, they say that it executes timers scheduled with timer and also process i/o events in poll queue. My confusion is taht we already have timer phase and i/o callback phase for those callbacks, then what is the work done by poll phase. It also says that thread may sleep in poll phase but I don't get it properly.
My questions are-

  1. Why poll phase is executing scripts for timers and i/o(s) when we already have timer and i/o callback phase ?
  2. Is it like poll phase executes callbacks on behalf of timer and i/o callback phase and timer and callback phase is only for internal processing no callbacks are executed in this phase ?
  3. Where can we place promises in this loop ? Earlier I thought that promises can be thought simply as callbacks and we can treat them like callbacks only, but in this video, he says that promises goes into an internal event loop, but does not talk in detail.

I am very confused at this point. Any help will be appreciated.

3 Answers 3

11

The poll phase boils down to an asynchronous I/O wait. Libuv will use different APIs depending on the OS but they all generally have the same pattern. I'm going to use select() as an example.

The poll is basically a system call like this:

select(maxNumberOfIO, readableIOList, writableIOList, errorIOList, timeout);

This function blocks. If no timeout value is specified it blocks forever.

The result is that node.js will not be able to execute any javascript as long as there is no I/O activity. This obviously makes it impossible to execute time-based callbacks like setTimeout() or setInterval().

Therefore, what node needs to do before calling such a function is to calculate what value to pass as timeout. It generally does this by going through the list of all timers and figure out the shortest amount of time it can wait for I/O (the next nearest timer) and use that as the timeout value. It basically processes all the timers but not to execute their callbacks, it does it to figure out the wait time.

Sign up to request clarification or add additional context in comments.

10 Comments

What if we are blocked "forever" but some I/O callback schedules a function with setImmediately? Would this waiting be interrupted?
There is no interrupting the program execution. The C interrupt handler (which is part of the OS - Windows, Linux or MacOS) will register an OS event which can be read later by javascript (node.js, Chrome, Safari, Firefox, Edge etc.) but the javascript engine itself is not interruptable. The interpreter will simply execute forever before attempting to check the OS event and since forever never ends the setImmediately callback never gets executed
And a few more I've probably forgotten. If you need code to execute on time regardless then you need a thread. For browsers use web workers and for node.js use worker threads
|
3

Nodejs has 5 major phases.
1) timers phase.
2) pending call back phase.
3) poll phase
4) check (set immediate).
5) close

Answer to your questions.
1)The call backs to timers and check phase are executed in their respective phases and not in poll phase.

2)All the I/o related call backs and other are executed in the poll phase. The pending call back phase is only for system level callbacks like tcp errors, none of our concern

3)After each phase, node js has an internal event loop which resolves all the process.nextTick callbacks, and another smaller event loop which executes the resolved promises then callbacks i.e Promise.resolve.then() callbacks.

1 Comment

"2)All the I/o related call backs and other are executed in the poll phase." - what is "other" here that you are referring to?
1

I was just reading about that myself. As far as the timers are concerned the documentation about the event loop gives a decent answer in the form of an example. Say a setTimeout timer is set to trigger after 100ms but an I/O process is in progress (in the polling phase) and requires more than 100ms to execute, say 150ms. Once it is finished the polling phase will then wrap back to the timer phase and execute the setTimeout later than the expected 100ms, at 150ms.

Hope that helps answer how the polling phase relates to the timer phase. In essence the polling phase, as I understand it, can 'make the decision' to run the timer phase again if necessary.

2 Comments

found this discussion on reddit that seems illuminating still trying to fully grok it reddit.com/r/node/comments/75m6cx/…
I find the whole "wrap back to the timer phase" confusing, does this mean, the event loop does a reverse ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.