How does the event loop work in Node.js?
The Node.js Event Loop
The Event Loop is the heart of Node.js. It's what allows Node.js to perform non-blocking I/O despite being single-threaded β by offloading operations to the OS and libuv's thread pool, then executing callbacks when they complete.
Event Loop Phases
The event loop processes callbacks in a strict order of phases:
βββββββββββββββββββββββββββββ
ββ>β timers β β setTimeout, setInterval callbacks
β βββββββββββββββ¬ββββββββββββββ
β βββββββββββββββ΄ββββββββββββββ
β β pending callbacks β β I/O callbacks deferred to next loop
β βββββββββββββββ¬ββββββββββββββ
β βββββββββββββββ΄ββββββββββββββ
β β idle, prepare β β internal use only
β βββββββββββββββ¬ββββββββββββββ
β βββββββββββββββ΄ββββββββββββββ
β β poll β β retrieve new I/O events
β βββββββββββββββ¬ββββββββββββββ
β βββββββββββββββ΄ββββββββββββββ
β β check β β setImmediate callbacks
β βββββββββββββββ¬ββββββββββββββ
β βββββββββββββββ΄ββββββββββββββ
ββββ€ close callbacks β β socket.on('close', ...)
βββββββββββββββββββββββββββββBetween each phase, Node.js runs microtasks:
process.nextTick()callbacks (highest priority)- Promise callbacks (
.then(),async/await)
Execution Order Example
console.log('1: start');
setTimeout(() => console.log('4: setTimeout'), 0);
setImmediate(() => console.log('5: setImmediate'));
Promise.resolve().then(() => console.log('3: Promise'));
process.nextTick(() => console.log('2: nextTick'));
console.log('1: end');
// Output:
// 1: start
// 1: end
// 2: nextTick
// 3: Promise
// 4: setTimeout
// 5: setImmediatePhase Details
1. Timers
Executes callbacks scheduled by setTimeout() and setInterval() whose delay threshold has passed.
2. Poll
Retrieves new I/O events. If nothing is queued, it waits here for I/O callbacks.
3. Check
Executes setImmediate() callbacks β always after I/O events.
Microtasks vs Macrotasks
| Type | Examples | Priority |
|---|---|---|
| Microtask | process.nextTick, Promises | Highest β runs between every phase |
| Macrotask | setTimeout, setInterval, I/O | Lower β runs in event loop phases |
Key Takeaway
The event loop allows Node.js to handle thousands of concurrent connections without creating a thread per connection. As long as you avoid blocking the main thread (e.g. with synchronous fs calls or CPU-heavy loops), Node.js remains highly performant.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.