Skip to content

Chapter 4 of 8

Asynchronous Programming

Asynchronous programming is at the heart of Node.js, and Promises are its primary abstraction. A Promise represents the eventual completion or failure of an asynchronous operation and has three states: pending, fulfilled, or rejected. Once a Promise settles, it stays settled forever. Code attaches success handlers with .then(), error handlers with .catch(), and cleanup logic with .finally(), allowing complex asynchronous flows to be expressed as readable chains.

The async/await syntax, introduced in modern JavaScript, makes working with Promises feel synchronous. An async function always returns a Promise, and the await keyword pauses execution inside the function until the awaited Promise resolves. This makes error handling natural with try/catch blocks and produces code that is easier to read and reason about than long chains of .then() calls.

When working with multiple Promises, JavaScript provides several combinators. Promise.all() takes an array of Promises and resolves when all of them succeed, rejecting on the first failure; it is ideal for running independent async operations concurrently for better performance. Promise.allSettled() waits for every Promise to either fulfill or reject without short-circuiting, which is useful when you want to know the outcome of each operation. Promise.race() resolves or rejects as soon as the first Promise settles, and Promise.any() resolves with the first fulfillment but only rejects if every Promise rejects. For older callback-based APIs, util.promisify() converts a function following the error-first callback convention into one that returns a Promise, allowing seamless integration with modern async/await code.

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...