All questions
Coding

How does the Node.js event loop work?

Node.js runs JavaScript on a single thread but stays responsive through an event loop plus non-blocking I/O. When your code starts an async operation (a file read, a network call), Node hands it off to the system (via libuv) and continues executing; when the operation finishes, its callback is queued to run.

The event loop processes these callbacks in phases on each iteration — timers (setTimeout/setInterval), pending I/O callbacks, poll (retrieving new I/O events), check (setImmediate), and close callbacks. Between phases, Node drains the microtask queue: resolved Promise callbacks and process.nextTick run before the loop moves on, which is why Promise callbacks fire ahead of setTimeout.

The key insight for an interview: the single thread never blocks on I/O, so one process can handle many concurrent connections. CPU-bound work, however, does block the loop — for that you offload to worker threads or separate processes.

Practise this live with NostrobeAI

Get real-time, structured guidance for coding, system design, and behavioral interviews. Free trial, no subscription.

Download Free Trial