Async context
asyncLocalStorage.run()
Runs a callback with a store available to asynchronous work created within that scope. The previous context is restored outside the call. Prefer run over enterWith for bounded request scope; the store object itself is not automatically immutable. Tested on Node.js 22.16.0. Example output (one line per entry): ["request-a", "true"].
Formal Syntax & Structure
nodejs syntaxnodejs
asyncLocalStorage.run(store, callback[, ...args])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| store | any | Context value. |
| callback | Function | Scoped operation. |
| args | any[] | Callback arguments. |
Return Value
any
Callback return value.
Working Example
Example Snippetnodejs
import { AsyncLocalStorage } from 'node:async_hooks';const als = new AsyncLocalStorage();try { await als.run('request-a', async () => { await Promise.resolve(); console.log(als.getStore()); }); console.log(als.getStore() === undefined);}finally { als.disable();}