Async Hooks in Node.js: A Complete Guide

The node:async_hooks module provides an API to track asynchronous resources in Node.js. . However, this API is experimental and its use is strongly discouraged due to usability issues, safety risks, and performance implications. For most use cases, AsyncLocalStorage or Diagnostics Channel are recommended alternatives. Still, understanding Async Hooks is useful for learning how asynchronous resources are created, executed, and destroyed.

async_hooksexecutionAsyncIdtriggerAsyncIdcreateHookAsyncHook Classinit / before / after / destroy / promiseResolve

~5 min read · Updated Dec 26, 2025

1. Introduction


Async Hooks allow developers to track the lifecycle of asynchronous resources such as Promises, TCP connections, and timers. Each resource is assigned a unique asyncId that identifies it within the Node.js process.

2. Core API


const async_hooks = require('node:async_hooks');

// Current execution context ID
const eid = async_hooks.executionAsyncId();

// ID of the resource that triggered the current one
const tid = async_hooks.triggerAsyncId();

// Create a new hook
const hook = async_hooks.createHook({ init, before, after, destroy, promiseResolve });
hook.enable();
hook.disable();

3. Hook Callbacks


  • init(asyncId, type, triggerAsyncId, resource): Called when a resource is initialized.
  • before(asyncId): Called just before the resource’s callback executes.
  • after(asyncId): Called immediately after the resource’s callback finishes.
  • destroy(asyncId): Called when the resource is destroyed.
  • promiseResolve(asyncId): Called when a Promise is resolved.

4. Error Handling


If any AsyncHook callback throws an error, Node.js prints the stack trace and exits. This strict behavior is due to the volatile nature of object lifecycles during construction and destruction.

5. Debugging Notes


Using console.log() inside AsyncHook callbacks causes infinite recursion because logging itself is asynchronous. Instead, use synchronous logging like fs.writeFileSync() for debugging.

const fs = require('node:fs');
const util = require('node:util');

function debug(...args) {
  fs.writeFileSync('log.out', `${util.format(...args)}\n`, { flag: 'a' });
}

6. The AsyncHook Class


The AsyncHook class exposes an interface for tracking lifecycle events of asynchronous operations. Hooks are disabled by default and must be explicitly enabled.

const async_hooks = require('node:async_hooks');
const hook = async_hooks.createHook(callbacks).enable();

7. Example: TCP Server Tracking


This example demonstrates tracking events in a TCP server:

const async_hooks = require('node:async_hooks');
const fs = require('node:fs');
const net = require('node:net');
const { fd } = process.stdout;

let indent = 0;
async_hooks.createHook({
  init(asyncId, type, triggerAsyncId) {
    const eid = async_hooks.executionAsyncId();
    fs.writeSync(fd, `${' '.repeat(indent)}${type}(${asyncId}): trigger: ${triggerAsyncId} execution: ${eid}\n`);
  },
  before(asyncId) {
    fs.writeSync(fd, `${' '.repeat(indent)}before: ${asyncId}\n`);
    indent += 2;
  },
  after(asyncId) {
    indent -= 2;
    fs.writeSync(fd, `${' '.repeat(indent)}after: ${asyncId}\n`);
  },
  destroy(asyncId) {
    fs.writeSync(fd, `${' '.repeat(indent)}destroy: ${asyncId}\n`);
  },
}).enable();

net.createServer(() => {}).listen(8080);

8. executionAsyncId vs triggerAsyncId


  • executionAsyncId(): Shows the context in which a resource was created.
  • triggerAsyncId: Shows which resource triggered the creation of the new resource.

Conclusion


Although async_hooks is experimental and discouraged for production use, it provides valuable insight into the lifecycle of asynchronous resources in Node.js. For context tracking, developers should prefer AsyncLocalStorage, which is stable and safer.

1. before(asyncId)


Called just before the callback of an asynchronous resource executes. asyncId is the unique identifier of the resource. This hook may be called 0 to N times:

  • Persistent resources like TCP servers call before multiple times.
  • One-off operations like fs.open() call it only once.

2. after(asyncId)


Called immediately after the callback specified in before completes. Even if an uncaught exception occurs, after runs once the exception is emitted.

3. destroy(asyncId)


Called when the resource is destroyed. If the resource relies on garbage collection, destroy may never be called, potentially causing memory leaks. Tracking destroy hooks adds overhead because it enables Promise tracking via the garbage collector.

4. promiseResolve(asyncId)


Called when the resolve() function of a Promise is invoked. This does not mean the Promise is fulfilled or rejected yet, only that resolution has been triggered.

new Promise((resolve) => resolve(true)).then(() => {});
// Calls: init, promiseResolve, before, after

5. executionAsyncResource()


Returns the resource representing the current execution. Typically, this is an internal Node.js handle object. Accessing undocumented properties may crash the application.

Example:

const { open } = require('node:fs');
const { executionAsyncId, executionAsyncResource } = require('node:async_hooks');

console.log(executionAsyncId(), executionAsyncResource()); // 1 {}
open(__filename, 'r', (err, fd) => {
  console.log(executionAsyncId(), executionAsyncResource()); // 7 FSReqWrap
});

6. executionAsyncId()


Returns the asyncId of the current execution context. This ID reflects execution timing, not causality.

const async_hooks = require('node:async_hooks');
console.log(async_hooks.executionAsyncId()); // 1 bootstrap

7. triggerAsyncId()


Returns the ID of the resource that triggered the current callback.

const server = net.createServer((conn) => {
  async_hooks.triggerAsyncId(); // ID of the connection
}).listen(port);

8. asyncWrapProviders


Introduced in v17.2.0, this returns a map of provider types to numeric IDs. It replaces the deprecated process.binding('async_wrap').Providers.

9. Promise Execution Tracking


By default, Promises do not receive asyncIds due to performance costs. Enabling hooks forces Promise tracking.

const { createHook, executionAsyncId, triggerAsyncId } = require('node:async_hooks');

createHook({ init() {} }).enable();
Promise.resolve(1729).then(() => {
  console.log(`eid ${executionAsyncId()} tid ${triggerAsyncId()}`);
});

In this example, the first Promise gets asyncId=6 and the chained Promise gets asyncId=7.

Conclusion


The before, after, destroy, and promiseResolve hooks provide detailed tracking of asynchronous resource lifecycles. Methods like executionAsyncResource, executionAsyncId, and triggerAsyncId are essential for identifying execution contexts and triggers. Although async_hooks is experimental and discouraged for production use, it remains a valuable tool for understanding Node.js asynchronous behavior.

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