Node.js Event Loop: nextTick vs setImmediate

Medium100% Free~18 mins#node-js#event-loop#process-nexttick#setimmediate#libuv
Interview Answer (30-Second Summary)

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');
2
3console.log('start');
4
5fs.readFile(__filename, () => {
6 setTimeout(() => console.log('timeout'), 0);
7 setImmediate(() => console.log('immediate'));
8 process.nextTick(() => console.log('nextTick in I/O'));
9});
10
11Promise.resolve().then(() => console.log('promise'));
12process.nextTick(() => console.log('nextTick'));
13
14console.log('end');
Predict Console Output
Interactive Challenge

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.

Call Stack (Top = Active)
main script
Lexical Scope / BindingsNo active local bindings
Console Stream
> 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.

  1. Between callbacks: after the main script and after every callback, Node drains the process.nextTick queue, then the promise microtask queue. That is why nextTick prints before promise. (In an ES module the top level already runs inside a promise job, so there the promise callback comes first.)
  2. Inside an I/O callback: the loop is in the poll phase. The next phase is check, so setImmediate always runs before a 0 ms setTimeout, which waits for the timers phase of the next iteration. In the main module that order is not guaranteed.
  3. 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.

Q1.
What is the event loop in Node.js?

It is the loop, provided by libuv, that lets single-threaded JavaScript handle I/O without blocking. Node hands slow work such as file reads to the operating system or libuv's thread pool, and the event loop runs the callbacks when results are ready.

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.

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.

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.

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).

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.

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