QA
Beginner LevelNode JS

How does the Node.js Event Loop work? Explain the 6 execution phases (Timers, Pending, Poll, Check, Close).

❓ Interview Prompt: Explain libuv Event Loop phases, microtask queue priority (process.nextTick, Promises), and non-blocking I/O execution.

Node.js runs single-threaded JavaScript execution backed by libuv for asynchronous I/O. The Event Loop coordinates asynchronous callbacks across 6 distinct phases:

6 Event Loop Phases:

1
Timers: Executes callbacks scheduled by 'setTimeout()' and 'setInterval()'.
2
Pending Callbacks: Executes I/O callbacks deferred to the next loop iteration.
3
Idle, Prepare: Used internally by libuv.
4
Poll: Retrieves new I/O events (file reads, network sockets); executes I/O callbacks.
5
Check: Executes callbacks scheduled by 'setImmediate()'.
6
Close Callbacks: Executes close handlers (e.g. 'socket.on('close')').

Microtask Queue Priority:

'process.nextTick()' and resolved 'Promises' execute immediately after the current operation finishes, BEFORE the event loop advances to the next phase!

Code Implementation Example

javascript
import fs from 'node:fs';

console.log('1. Script Start');

setTimeout(() => console.log('2. setTimeout (Timers Phase)'), 0);
setImmediate(() => console.log('3. setImmediate (Check Phase)'));

process.nextTick(() => console.log('4. process.nextTick (Microtask)'));
Promise.resolve().then(() => console.log('5. Promise.then (Microtask)'));

fs.readFile(__filename, () => {
  console.log('6. I/O Callback (Poll Phase)');
});

console.log('7. Script End');
💡Node.js Event Loop phase execution order demonstration.
🎯 Interview Key Takeaway

Microtasks (process.nextTick and Promises) execute immediately after the current operation completes, before the event loop advances phases.

#Node.js#Event Loop#libuv#Asynchronous#Beginner
Back to all Node JS Questions→

More Node JS Interview Questions

Beginner LevelNode JS

What does 'Single-Threaded, Event-Driven, Non-Blocking I/O' mean in Node.js?

Beginner LevelNode JS

What is the difference between process.nextTick() and setImmediate()?

Beginner LevelNode JS

Compare CommonJS (require/module.exports) vs ES Modules (import/export) in Node.js.