Asynchronous Context Tracking in Node.js

The node:async_hooks module provides tools for tracking and managing asynchronous contexts in Node.js. . Its two main classes, AsyncLocalStorage and AsyncResource, allow developers to store and propagate state across callbacks and promise chains. This functionality is similar to thread-local storage in other languages and is essential for managing state across the lifecycle of asynchronous operations such as web requests.

AsyncLocalStorageAsyncResourceContext Propagationasync_hooksWorker PoolEventEmitter Integration

~3 min read · Updated Dec 26, 2025

1. AsyncLocalStorage


The AsyncLocalStorage class creates storage that remains consistent across asynchronous operations. It is the recommended way to manage context because it is performant and memory-safe compared to custom implementations.

Example: Request Logger

const { AsyncLocalStorage } = require('node:async_hooks');
const http = require('node:http');

const asyncLocalStorage = new AsyncLocalStorage();

function logWithId(msg) {
  const id = asyncLocalStorage.getStore();
  console.log(`${id !== undefined ? id : '-'}:`, msg);
}

let idSeq = 0;
http.createServer((req, res) => {
  asyncLocalStorage.run(idSeq++, () => {
    logWithId('start');
    setImmediate(() => {
      logWithId('finish');
      res.end();
    });
  });
}).listen(8080);

Each instance of AsyncLocalStorage maintains its own independent context, so multiple instances can coexist safely.

2. Key AsyncLocalStorage Methods


  • run(store, callback): Runs a function within a given context.
  • getStore(): Retrieves the current store.
  • enterWith(store): Enters a context for synchronous execution.
  • exit(callback): Exits the context and runs a function outside of it.
  • disable(): Disables the instance for garbage collection.
  • bind(fn) and snapshot(): Bind or capture the current context.

3. AsyncResource


The AsyncResource class is designed to be extended for custom asynchronous resources. It allows developers to trigger lifecycle events and associate operations with the correct execution context.

Key Features:

  • runInAsyncScope(fn, thisArg, ...args): Executes a function in the resource’s context.
  • emitDestroy(): Calls destroy hooks.
  • asyncId(): Returns the unique ID of the resource.
  • triggerAsyncId(): Returns the ID of the resource that created it.

Example:

class DBQuery extends AsyncResource {
  constructor(db) {
    super('DBQuery');
    this.db = db;
  }

  getInfo(query, callback) {
    this.db.get(query, (err, data) => {
      this.runInAsyncScope(callback, null, err, data);
    });
  }

  close() {
    this.db = null;
    this.emitDestroy();
  }
}

4. Worker Pool Example


AsyncResource can be used to track tasks in a Worker pool, ensuring callbacks are correctly associated with tasks rather than worker creation.

class WorkerPoolTaskInfo extends AsyncResource {
  constructor(callback) {
    super('WorkerPoolTaskInfo');
    this.callback = callback;
  }

  done(err, result) {
    this.runInAsyncScope(this.callback, null, err, result);
    this.emitDestroy();
  }
}

This model can be applied to database connection pools or other resource pools.

5. Integration with EventEmitter


EventEmitter listeners may run in a different context than the one active when on() was called. Using AsyncResource.bind() ensures listeners run in the correct context.

req.on('close', AsyncResource.bind(() => {
  // Execution context bound correctly
}));

Conclusion


The async_hooks module in Node.js is a powerful tool for managing asynchronous contexts. With AsyncLocalStorage and AsyncResource, developers can propagate state across async operations, build custom resources, and prevent context loss. These capabilities are vital for building scalable and reliable web applications.

Written & researched by Dr. Shahin Siami

Related Articles

Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

Continue

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

راهنمای جامع UDP / Datagram Sockets در Node.js

ماژول node:dgram پیاده‌سازی کامل سوکت‌های UDP را در Node.js فراهم می‌کند. UDP یک پروتکل سبک، بدون اتصال (connectionless) و مناسب برای برنامه‌های بلادرنگ مانند VoIP، بازی‌ها، IoT، سیستم‌های پخش (broadcast) و چندپخشی (multicast) است. این ماژول امکان ساخت سوکت، ارسال و دریافت دیتاگرام، مدیریت TTL، عضویت در گروه‌های multicast، کنترل بافرها، و مدیریت رفتار سطح پایین شبکه را فراهم می‌کند.

Continue