Request tracing
Attach correlation or request identifiers to asynchronous server work without threading them through every function parameter.
Node.js · Class
AsyncLocalStorage maintains context-specific state across asynchronous callbacks and promise chains. Use run() or enterWith() to establish a store and getStore() to retrieve it.
AsyncLocalStorage provides per-instance state that follows asynchronous callbacks and promise chains. Create a context with run() or enterWith(), then retrieve the active store with getStore(); run() confines the store to its callback and asynchronous work created there.
new AsyncLocalStorage([options]); asyncLocalStorage.disable(); asyncLocalStorage.getStore(); asyncLocalStorage.enterWith(store); asyncLocalStorage.run(store, callback[, ...args]);| Name | Type | Description |
|---|---|---|
options | object | Optional configuration object accepted by the constructor in the current API. |
store | any | The value to make current for the context. |
callback | Function | The function executed inside the context. |
args | any[] | The value to supply to the callback. |
The constructor returns an AsyncLocalStorage instance; getStore() returns the active store or undefined; run() returns the callback's return value; enterWith() and disable() return undefined.
import { AsyncLocalStorage } from 'node:async_hooks';
const requestContext = new AsyncLocalStorage<{ requestId: string }>();
function handleRequest(requestId: string) {
return requestContext.run({ requestId }, async () => {
await Promise.resolve();
return requestContext.getStore()?.requestId;
});
}
handleRequest('req-42').then(console.log);Creates a request context, preserves it across an asynchronous boundary, and reads the request ID from the active store.
Attach correlation or request identifiers to asynchronous server work without threading them through every function parameter.
Make request-scoped logging metadata available throughout callbacks and promise chains.
Propagate transaction, authentication, or tenant state across asynchronous operations.
AsyncLocalStorage associates a store with the current asynchronous execution context. The context propagates through callbacks and promise chains, allowing request-scoped data to be read without passing it through every function.
Follow-up: How do you initialize and read the context? Use run(store, callback) or enterWith(store), then call getStore() inside the callback or propagated asynchronous work.
run() executes a callback synchronously within a supplied context and returns the callback's result. The store is available to asynchronous operations created by that callback, but not to code outside its scope.
Follow-up: When would enterWith() be used? It changes the context for the rest of the current synchronous execution and carries it into subsequent asynchronous calls.
Each AsyncLocalStorage instance has its own independent storage context, so separate instances can coexist without sharing data. getStore() returns the active store, or undefined when no initialized context is present.
Follow-up: How can an instance be reset? Call disable(); getStore() then returns undefined until run() or enterWith() is used again.
AsyncResourceasync_hooksexecutionAsyncResourceexecutionAsyncId