tezvyn:

Order of the Node.js event loop phases

AI-drafted, machine-checkedSource: interviewintermediate
WHAT IT TESTS

understanding of libuv's loop, not just async vibes.

OUTLINE

timers, pending callbacks, poll, check, close phases in order; I/O completion runs in poll.

RED FLAG

claiming Node is single-phase or fully multithreaded.

WHAT THIS TESTS The interviewer wants to know if you understand that Node concurrency is driven by libuv's event loop with distinct ordered phases, rather than a vague single async queue. Senior engineers should map callbacks to the phase that fires them.

A GOOD ANSWER COVERS The loop cycles through phases in a fixed order on each tick. Timers fires callbacks whose setTimeout or setInterval threshold has elapsed. Pending callbacks handles a few deferred system operations such as certain TCP errors. Idle and prepare are internal. The poll phase is the heart of it: it calculates how long to block, retrieves completed I/O events, and executes the bulk of I/O callbacks like file reads and network responses. The check phase runs setImmediate callbacks. The close callbacks phase handles things like socket close events. Between every phase, and after each callback in some cases, the microtask queues drain: process.nextTick first, then resolved Promise jobs.

COMMON WRONG ANSWERS Claiming I/O callbacks run during the timers phase, describing Node as having one undifferentiated callback queue, or asserting Node is multithreaded for JavaScript execution. Another mistake is forgetting that the poll phase will block and wait for incoming I/O when no timers are due and no immediates are queued.

LIKELY FOLLOW-UPS Expect questions on where setImmediate runs versus setTimeout with zero delay, how process.nextTick can starve the loop, and how the thread pool relates to the loop for file system and crypto work.

ONE CONCRETE EXAMPLE A request arrives and triggers a database read. The handler returns immediately and the loop keeps spinning. When the database responds, libuv surfaces that completion during the poll phase, and your callback runs there to send the HTTP response. A setImmediate scheduled inside that callback would then run in the very next check phase of the same loop iteration.

Read the original → nodejs.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.