
Poll Phase Callbacksĭuring the poll phase, all non-blocking I/O request callbacks are executed. Any microtasks that are needed will be executed, and the event loop will continue to the check process. The poll phase, however, will come to an end if timers are set. If the poll queue is empty, it normally blocks and waits for any in-flight I/O operations to finish before executing their callbacks.

If one or more timers are ready, the event loop returns to the timers phase to perform the callbacks for those timers.

The event loop will search for timers whose time limits have been reached until the poll queue is empty. If setImmediate() was not used to schedule scripts, the event loop would wait for callbacks to be added to the queue before executing them.If the poll queue is not empty, the event loop will iterate through its callback queue, running them synchronously until the queue is exhausted or the system-dependent hard limit is met.If there are no timers scheduled when the event loop reaches the poll phase, one of two things will happen: Then, process events in the poll queue.Figuring out how long it should block and poll for I/O.
#Nodejs event loop code#
When the event loop is in the poll phase of its iteration, the entire code will be executed. It can execute immediately or add anything to the queue to be executed during a future tick of the Event Loop, depending on the code.Īssume that you have written the following code: This is the phase in which we run all of the JavaScript code we've written, starting at the top and working our way down. Polling Phase:Īll of our input/output operations are handled by the poll phase. A program can only run for as long as tasks are queued in the Event Loop or on the call stack. When there is no more pending work in the Event Loop, or when process.exit() is manually named a Node.js process exits. These six phases combine to form a tick, which is a cycle or loop. ).Īs Node.js starts executing your index.js file, or some other application entry point, the Event Loop begins. close callbacks: some close callbacks such as socket.on('close'.check: setImmediate() callbacks are called here.poll: fetch new I/O events execute I/O related callbacks (almost all with the exception of close callbacks, the ones scheduled by timers, and setImmediate()) the node will block here when required.pending callbacks: runs I/O callbacks that have been delayed until the next loop iteration.timers: callbacks scheduled by setTimeout() and setInterval() are executed in this phase.

#Nodejs event loop full#
The event loop will switch to the next phase when the queue is full or the callback limit is reached, and so on. While each process is unique in its own way, when the event loop enters one, it will perform any operations relevant to that phase before executing callbacks in that phase's queue until the queue is empty or the maximum number of callbacks has been executed.

There is a FIFO queue of callbacks to execute in each phase. When Node.js starts, it initializes the event loop, processes the given input script (or drops into the REPL) which may perform async API calls, schedule timers, or call process.nextTick(), then starts processing the event loop. It explains the mechanisms that make Node such a common, efficient, and effective modern system. It allows you to learn about Node's asynchronous processes and non-blocking I/O. The event loop is a core feature of Node.js.
