Node.js Event Loop Interview Questions Common Questions and Answers
Master the most common Node.js event loop interview questions microtasks vs macrotasks, setTimeout vs process.nextTick, and how the event loop handles I/O.
Node.js Event Loop Interview Questions
The event loop is the most frequently asked topic in Node.js interviews. Here are the most common questions and answers.
Q1: What Is the Event Loop?
Answer: The event loop is a mechanism that allows Node.js to perform non-blocking I/O operations despite being single-threaded. It offloads operations to the system kernel whenever possible and processes the results via callbacks.
┌───────────────────────────┐
│ Call Stack (V8) │
└───────────┬───────────────┘
│
┌───────────▼───────────────┐
│ Node APIs (libuv) │
│ (timers, I/O, etc.) │
└───────────┬───────────────┘
│
┌───────────▼───────────────┐
│ Event Loop │
│ ┌─────┐ ┌─────┐ ┌─────┐ │
│ │timer││ I/O │ │close│ │
│ └─────┘ └─────┘ └─────┘ │
└───────────────────────────┘
Q2: What Are the Phases of the Event Loop?
Answer: The event loop has 4 main phases:
- Timers Executes callbacks from setTimeout and setInterval
- Poll Retrieves new I/O events and executes their callbacks
- Check Executes callbacks from setImmediate
- Close Executes close event callbacks
Between each phase, the event loop processes microtasks (process.nextTick and Promise callbacks).
Q3: setTimeout vs setImmediate vs process.nextTick
Answer:
setTimeout(() => console.log("setTimeout"), 0); setImmediate(() => console.log("setImmediate")); process.nextTick(() => console.log("nextTick")); Promise.resolve().then(() => console.log("Promise")); // Output order: // nextTick // Promise // setTimeout (or setImmediate order varies) // setImmediate (or setTimeout)
- process.nextTick Runs after the current operation, before the next phase. Highest priority.
- Promise.then Microtask, runs after nextTick, before the next phase.
- setTimeout Macrotask, runs in the timers phase.
- setImmediate Macrotask, runs in the check phase.
Q4: Why Is Node.js Single-Threaded?
Answer: Node.js uses a single thread for JavaScript execution (V8), but it uses libuv's thread pool (4 threads by default) for I/O operations. This design:
- Reduces context switching overhead
- Avoids thread synchronization complexity
- Handles thousands of concurrent connections efficiently
- Uses non-blocking I/O to maximize throughput
Q5: What Is the Difference Between process.nextTick and setImmediate?
Answer:
- process.nextTick Fires immediately after the current operation, before I/O events. Can starve I/O if used excessively.
- setImmediate Fires in the next iteration of the event loop, after I/O events. Safe for recursive calls.
process.nextTick(() => console.log("nextTick")); setImmediate(() => console.log("setImmediate")); // nextTick always fires first
Q6: What Happens in This Code?
for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); } // Output: 3, 3, 3 (not 0, 1, 2)
Answer: var is function-scoped (not block-scoped). By the time setTimeout callbacks run, the loop has finished and i is 3. Fix with let (block-scoped):
for (let i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); } // Output: 0, 1, 2
The Takeaway
Event loop interview questions cover: phases (timers, poll, check, close), microtasks vs macrotasks (nextTick and Promises run before timers and setImmediate), single-threaded design with libuv thread pool, setTimeout vs setImmediate vs process.nextTick ordering, and closure-related gotchas with var vs let. Practice these questions to ace Node.js interviews.
The event loop allows Node.js to perform non-blocking I/O despite being single-threaded. It offloads I/O to the system kernel and processes results via callbacks. It has phases: timers (setTimeout/setInterval), poll (I/O events), check (setImmediate), and close. Microtasks (nextTick, Promises) run between phases.
process.nextTick fires first (immediately after current operation), then Promise.then (microtask), then setTimeout and setImmediate (order varies in the same context, but setImmediate always fires after I/O in an I/O callback).
Node.js uses a single thread for JavaScript execution (V8) to avoid context switching and thread synchronization complexity. It uses libuv's thread pool (4 threads by default) for I/O operations and non-blocking I/O to handle thousands of concurrent connections efficiently.
process.nextTick fires immediately after the current operation, before I/O events (can starve I/O if overused). setImmediate fires in the next event loop iteration, after I/O events (safe for recursive calls). nextTick always fires before setImmediate.
Four main phases: timers (setTimeout/setInterval callbacks), poll (I/O events and callbacks), check (setImmediate callbacks), and close (close event callbacks). Between each phase, microtasks (process.nextTick and Promise callbacks) are processed.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

