How does the Node.js event loop work?
Intermediateevent-loopasyncnon-blockinglibuv
Full Answer
The Node.js event loop allows Node.js to perform non-blocking I/O operations despite JavaScript being single-threaded. It offloads operations to the system kernel or libuv thread pool and resumes execution when they complete.
Phases of the event loop (in order):
- timers — executes
setTimeoutandsetIntervalcallbacks - pending callbacks — I/O callbacks deferred from the previous iteration
- idle, prepare — internal use only
- poll — retrieves new I/O events; blocks here if queue is empty
- check — executes
setImmediatecallbacks - close callbacks — handles
closeevents
Between each phase, Node.js drains process.nextTick and Promise microtask queues completely.
Quick Answer for Interviewer
The event loop processes callbacks across 6 phases, draining microtasks (nextTick, Promises) between each. It allows Node.js to handle concurrent I/O without threads by deferring work to the OS/libuv.
Flashcard