Skip to content

Chapter 1 of 8

Foundations of Node.js and the Event Loop

Node.js is a JavaScript runtime built on Chrome's V8 engine that executes JavaScript outside the browser. Its defining characteristic is an event-driven, non-blocking I/O model, which makes it lightweight and efficient for building scalable network applications. Despite JavaScript being single-threaded, Node.js achieves concurrency through the event loop, a mechanism that continuously checks the call stack and the callback queue. When the call stack is empty, the event loop pulls callbacks from the queue and executes them, allowing I/O operations to proceed without blocking the main thread.

The event loop progresses through a sequence of phases on each iteration: timers, pending callbacks, idle and prepare, poll, check, and close callbacks. The timers phase runs callbacks scheduled with setTimeout and setInterval. The poll phase retrieves new I/O events and may block briefly while waiting for them. The check phase then executes setImmediate callbacks, and the close callbacks phase handles things like socket close events. Understanding this ordering is essential for predicting when asynchronous code will actually run.

Two commonly confused scheduling primitives are setImmediate and setTimeout(fn, 0). setImmediate fires in the check phase of the current iteration, after the poll phase completes, while setTimeout schedules the callback for the timers phase on the next iteration. Inside an I/O callback, setImmediate always fires before setTimeout. A separate primitive, process.nextTick, fires even sooner: it runs immediately after the current operation on the microtask queue, before the event loop continues at all. Although useful, overusing process.nextTick can starve I/O by recursively deferring the loop from moving forward.

All chapters
  1. 1Foundations of Node.js and the Event Loop
  2. 2Module Systems
  3. 3Streams and Buffers
  4. 4Asynchronous Programming
  5. 5Building Web Applications with Express
  6. 6Core Built-in Modules
  7. 7Package Management and Project Configuration
  8. 8Error Handling, Process Management, and Events

Drill it

Reading is not remembering. These come from the Nodejs Runtime deck:

Q

What is Node.js?

Node.js is a JavaScript runtime built on Chrome's V8 engine. It allows running JavaScript outside the browser, using an event-driven, non-blocking I/O model tha...

Q

What is the Node.js event loop?

The event loop is the mechanism that allows Node.js to perform non-blocking I/O operations despite JavaScript being single-threaded. It continuously checks the...

Q

What are the phases of the Node.js event loop?

Timers – executes setTimeout and setInterval callbacksPending callbacks – I/O callbacks deferred to next iterationPoll – retrieves new I/O eventsCheck – execute...

Q

What is the difference between setImmediate() and setTimeout()?

setImmediate() executes in the check phase of the event loop, after the poll phase completes. setTimeout(fn, 0) executes in the timers phase on the next iterati...