Asynchronous Node.js in Node.js
Asynchronous Node.js is the second chapter of the free Node.js tutorial. It shows how one thread keeps many jobs going, how to build your own events, and how to handle errors well.
What each lesson covers:
- Event Loop and Timers: How Node.js runs many jobs on one thread: blocking and non-blocking code, I/O-bound and CPU-bound work, the call stack, libuv and the thread pool, the event loop phases, timers, setImmediate, timers/promises, process.nextTick and the microtask queue, predicting output order, and how to avoid blocking the event loop.
- Events and Utilities: The observer pattern and EventEmitter: on, once, off, emit, prependListener, listener helpers, synchronous emit, arguments, the error event, captureRejections, listener limits, extending EventEmitter, events.once and events.on. Error-first callbacks, the util module (promisify, callbackify, inspect, format, types, isDeepStrictEqual, deprecate), cancelling work with AbortController and AbortSignal, and AsyncLocalStorage.
- Error Handling: Reading Node.js errors and stack traces, error.cause, system errors with code, errno, syscall and path, common error codes, Node.js ERR_ codes, operational versus programmer errors, thrown exceptions, rejected promises and error events, custom error classes, and process-level handlers: uncaughtException, unhandledRejection, the warning event and the fail-fast strategy.
Every lab runs a small Node.js runtime in your browser, in JavaScript or TypeScript (pick one in the Language menu at the top). Examples that need the real operating system show output from real Node.js.
Lessons in this section
- Event Loop and Timers
How Node.js runs many jobs on one thread: blocking and non-blocking code, I/O-bound and CPU-bound work, the call stack, libuv and the thread pool, the event loop phases, timers, setImmediate, timers/promises, process.nextTick and the microtask queue, predicting output order, and how to avoid blocking the event loop.
- Tell blocking and non-blocking code apart
- Name the main phases of the event loop
- Use setTimeout, setInterval, setImmediate and timers/promises
- Predict the order of nextTick, promises and timers
- Events and Utilities
The observer pattern and EventEmitter: on, once, off, emit, prependListener, listener helpers, synchronous emit, arguments, the error event, captureRejections, listener limits, extending EventEmitter, events.once and events.on. Error-first callbacks, the util module (promisify, callbackify, inspect, format, types, isDeepStrictEqual, deprecate), cancelling work with AbortController and AbortSignal, and AsyncLocalStorage.
- Create, listen to and emit events with EventEmitter
- Handle the error event and avoid listener leaks
- Build your own class on top of EventEmitter
- Turn error-first callbacks into promises with util.promisify
- Error Handling
Reading Node.js errors and stack traces, error.cause, system errors with code, errno, syscall and path, common error codes, Node.js ERR_ codes, operational versus programmer errors, thrown exceptions, rejected promises and error events, custom error classes, and process-level handlers: uncaughtException, unhandledRejection, the warning event and the fail-fast strategy.
- Read a Node.js stack trace from top to bottom
- Use code, errno, syscall and path on system errors
- Tell operational errors from programmer errors
- Handle thrown errors, rejected promises and error events