6 phases of the Event Loop in Node.js execute in a specific sequential order, but their behavior is influenced by the tasks queued in each phase and the priority of microtasks (like process.nextTick and Promises).
Here’s the sequential order of the Event Loop phases and how they operate:
1. Timers Phase
- What Happens: Executes callbacks scheduled by
setTimeoutandsetIntervalif their delay has expired. - Key Note: If there are no due timers, the Event Loop immediately moves to the next phase.
2. Pending Callbacks Phase
- What Happens: Processes I/O-related callbacks deferred from the previous cycle (e.g., TCP errors or operations like
socket.on('error')). - Key Note: This phase is mostly used internally by Node.js and is less visible in application-level code.
3. Idle, Prepare Phase
- What Happens: Used internally by Node.js for preparing the next iteration of the Event Loop.
- Key Note: Not directly accessible to developers, so you usually don’t interact with this phase.
4. Poll Phase
- What Happens:
- Retrieves new I/O events (e.g., reading files, network responses).
- Executes I/O callbacks if there are pending ones in the queue.
- If no I/O callbacks are pending:
- The Event Loop will either block and wait for new events or move to the Check Phase if
setImmediatecallbacks are pending.
- The Event Loop will either block and wait for new events or move to the Check Phase if
- Key Note: This is one of the most important phases for handling I/O.
5. Check Phase
- What Happens: Executes callbacks registered by
setImmediate. - Key Note:
setImmediateis processed here, ensuring it runs after the Poll Phase.
6. Close Callbacks Phase
- What Happens: Executes callbacks for resources that are closed (e.g.,
socket.on('close')). - Key Note: Typically used for cleanup operations.
Microtasks Queue
While the 6 phases are processed sequentially, microtasks have higher priority and can interrupt the Event Loop after any phase.
Microtasks include:
process.nextTick- Promise resolution handlers
At the end of each phase:
- Node.js processes all microtasks in the microtask queue.
- Once the microtask queue is cleared, the Event Loop proceeds to the next phase.
Does the Sequence Always Apply?
Yes, the phases are always processed in this sequential order, but:
- Phases may be skipped:
- If a phase has no queued callbacks or tasks (e.g., no timers in the Timers Phase), the Event Loop immediately moves to the next phase.
- Microtasks interrupt the sequence:
- Before transitioning to the next phase, Node.js will process all pending microtasks.
Key Observations
setTimeoutvssetImmediate:setTimeoutis executed in the Timers Phase.setImmediateis executed in the Check Phase.- If both are queued without delays,
setImmediatewill usually execute first because the Poll Phase processes I/O and moves to the Check Phase before timers.
process.nextTickvs Promises:- Both are microtasks, but
process.nextTickhas higher priority than Promises and executes before them.
- Both are microtasks, but
Conclusion
The 6 phases of the Event Loop are processed sequentially, but the microtask queue introduces interruptions between phases, ensuring high-priority tasks like process.nextTick and Promises are handled promptly. This strict order with interleaved microtasks is what enables Node.js to efficiently handle asynchronous tasks on a single thread.








Leave a comment