How does the Node.js event loop work, and what are its phases?
Node runs your JavaScript on one thread and hands I/O to the operating system through libuv. The event loop is what decides which completed callback runs next, and it cycles through fixed phases:
- timers — callbacks scheduled by
setTimeoutandsetIntervalwhose time has come. - pending callbacks — some system-level callbacks deferred from the previous cycle.
- idle, prepare — internal use only.
- poll — the important one. Node retrieves new I/O events and runs their callbacks, and will block here waiting for work if there is nothing else to do.
- check —
setImmediatecallbacks. - close callbacks —
'close'events, such as a socket being destroyed.
Between every phase, and between individual callbacks, Node drains two queues completely: process.nextTick first, then the promise microtask queue.
Note: The consequence worth stating is that any CPU-heavy synchronous work blocks the entire loop — no other request is served while it runs. That is why hashing, image processing, or large JSON parsing belong in a worker thread or a separate process.





