- JavaScript
- The Event Loop, Microtasks, and Asynchronous Concurrency
- Node.js Event Loop
Node.js Event Loop: nextTick vs setImmediate
In JavaScript, the Node.js event loop is libuv's loop that runs callbacks in phases: timers, pending callbacks, poll (I/O), check (setImmediate) and close callbacks. After the current callback, Node drains the process.nextTick queue and then the promise microtask queue before moving on, so nextTick runs before promises, and inside an I/O callback setImmediate always runs before a 0 ms setTimeout.
Key Learning Objectives
Name the phases of the Node.js event loop and what runs in each.
Predict the order of process.nextTick, promise callbacks, setImmediate and setTimeout.
Explain why setImmediate beats a 0 ms setTimeout inside an I/O callback but not in the main module.
Describe what the libuv thread pool does and why Node is still single-threaded for your code.
The Interview Problem
Run as a CommonJS script in Node, in what order are the seven lines printed?
1const fs = require('fs');23console.log('start');45fs.readFile(__filename, () => {6 setTimeout(() => console.log('timeout'), 0);7 setImmediate(() => console.log('immediate'));8 process.nextTick(() => console.log('nextTick in I/O'));9});1011Promise.resolve().then(() => console.log('promise'));12process.nextTick(() => console.log('nextTick'));1314console.log('end');
Predict Console Output
Select the option that matches what this code prints to the console:
start / end / nextTick / promise / nextTick in I/O / immediate / timeout
start / end / promise / nextTick / nextTick in I/O / immediate / timeout
start / end / nextTick / promise / nextTick in I/O / timeout / immediate
start / nextTick / promise / end / nextTick in I/O / immediate / timeout
V8 Engine Execution Trace
Step 1 of 10 (Line 3)The main script runs top to bottom and logs start.
Node.js Event Loop Explained
How the Node.js Event Loop Orders Callbacks
Node runs your JavaScript on one thread and uses libuv for I/O. The event loop goes through phases in a fixed order: timers (setTimeout, setInterval), pending callbacks, poll (I/O callbacks), check (setImmediate) and close callbacks.
- Between callbacks: after the main script and after every callback, Node drains the
process.nextTickqueue, then the promise microtask queue. That is whynextTickprints beforepromise. (In an ES module the top level already runs inside a promise job, so there the promise callback comes first.) - Inside an I/O callback: the loop is in the poll phase. The next phase is check, so
setImmediatealways runs before a 0 mssetTimeout, which waits for the timers phase of the next iteration. In the main module that order is not guaranteed. - Thread pool: file system calls,
dns.lookup, crypto and zlib run on libuv's thread pool (4 threads by default), so a slow read does not block the loop; only the callback comes back to the main thread.
Common Traps & Mistakes
Assuming setTimeout(fn, 0) always runs before setImmediate; it depends on where they are scheduled.
Treating process.nextTick as a promise microtask. It has its own queue, drained first in CommonJS.
Calling process.nextTick recursively: the loop cannot move on until the queue is empty, so I/O starves.
FAANG Follow-Up Probes
Probe #1
What happens if a nextTick callback schedules another nextTick, again and again?
Probe #2
Why can a CPU-heavy synchronous loop make an HTTP server stop responding, and how would you fix it?
Probe #3
When would you choose setImmediate over process.nextTick in a library?
Node.js Event Loop Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for Node.js Event Loop.
What are the phases of the Node.js event loop?
Each iteration goes through timers (setTimeout, setInterval), pending callbacks, idle/prepare (internal), poll (new I/O events and their callbacks), check (setImmediate) and close callbacks. Between callbacks Node drains the nextTick and promise microtask queues.
What is the difference between process.nextTick and setImmediate?
process.nextTick runs its callback right after the current operation, before promises and before the loop continues. setImmediate queues the callback for the check phase of the event loop, after the poll phase.
Does process.nextTick run before promises?
In a CommonJS script, yes: Node drains the whole nextTick queue before the promise microtask queue. In an ES module the top-level code itself runs inside a promise job, so a promise callback queued there runs before a nextTick callback; running the three-line test as .mjs prints end, promise, nextTick.
Which runs first, setTimeout(fn, 0) or setImmediate?
Inside an I/O callback, setImmediate always runs first, because the check phase comes straight after poll. From the main module the order is not guaranteed: it depends on whether the 1 ms timer threshold has passed when the loop starts (in 20 runs on Node 24 we saw immediate first 19 times and timeout first once).
How is the Node.js event loop different from the browser event loop?
Both run one callback at a time and drain microtasks after each one. Node adds explicit phases, setImmediate, process.nextTick and the libuv thread pool; the browser has rendering steps and requestAnimationFrame instead.
Is Node.js single-threaded?
Your JavaScript runs on one thread. libuv keeps a thread pool, 4 threads by default (UV_THREADPOOL_SIZE), for work such as file system calls, dns.lookup, crypto and zlib, and network I/O uses the operating system's non-blocking APIs.
More JavaScript interview questions for freshers
