Node.js module
async_hooks
The 'node:async_hooks' module provides hooks to track the lifecycle of asynchronous resources in Node.js—init, before, after, and destroy—for tasks like promises, timers, callbacks, and I/O operations.
It's useful for keeping state across async callbacks, measuring performance, or building custom logging and tracing tools in your application.
Works in Bun · notes
AsyncLocalStorage and AsyncResource are implemented. However, v8 promise hooks required for broader compatibility are not called.
Functions 4
- async_hooks.createHook()
Registers functions to be called for different lifetime events of each async operation.
- async_hooks.executionAsyncId()
``js import { executionAsyncId } from 'node:async_hooks'; import fs from 'node:fs' - async_hooks.executionAsyncResource()
Resource objects returned by
executionAsyncResource()are most often internal - async_hooks.triggerAsyncId()
``js const server = net.createServer((conn) => { // The resource that caused (or triggered) this callback to be called // was that of the new connection
Classes 2
- class async_hooks.AsyncLocalStorage
This class creates stores that stay coherent through asynchronous operations.
- class async_hooks.AsyncResource
The class
AsyncResourceis designed to be extended by the embedder's async resources.
Interfaces 5
- interface AsyncHook
- interface AsyncLocalStorageOptions
- interface AsyncResourceOptions
- interface HookCallbacks
- interface RunScope
A disposable scope returned by
asyncLocalStorage.withScope()that automatically restores the previous store value when disposed.