node:async_hooks
Node-compatible async context propagation (AsyncLocalStorage, AsyncResource,
executionAsyncId, triggerAsyncId) (node:async_hooks).
Available in vjs only (not available in vjs-lite). Enabled by default
unless --no-node-compat is passed.
import ... from "node:async_hooks"
node-compat (enabled by default in vjs; absent in vjs-lite; disable with --no-node-compat)Example
Section titled “Example”import { AsyncLocalStorage } from "node:async_hooks";
const storage = new AsyncLocalStorage<{ requestId: string }>();const result = await storage.run({ requestId: "req-1" }, async () => { return storage.getStore()?.requestId;});console.log(result);Functions
Section titled “Functions”executionAsyncId
Section titled “executionAsyncId”export function executionAsyncId(): number;
Return the unique numeric ID of the current async execution context.
number — Current execution async ID (0 when outside an async scope).triggerAsyncId
Section titled “triggerAsyncId”export function triggerAsyncId(): number;
Return the async ID of the context that scheduled the current execution.
number — Triggering async ID.Classes
Section titled “Classes”AsyncLocalStorage
Section titled “AsyncLocalStorage”export class AsyncLocalStorage<T = unknown> {
readonly name: string;
constructor(options?: AsyncLocalStorageOptions<T>);
disable(): void;
getStore(): T | undefined;
run<R, TArgs extends any[] = any[]>(
store: T,
callback: (...args: TArgs) => R,
...args: TArgs
): R;
exit<R, TArgs extends any[] = any[]>(callback: (...args: TArgs) => R, ...args: TArgs): R;
enterWith(store: T): void;
static bind<Func extends (...args: any[]) => any>(fn: Func): Func;
static snapshot(): <R, TArgs extends any[]>(
fn: (...args: TArgs) => R,
...args: TArgs
) => R;
}
Asynchronous context store preserving values across promises, timers, and async callbacks.
constructor(options?: AsyncLocalStorageOptions<T>);
Construct a new AsyncLocalStorage instance.
| Name | Type | Description |
|---|---|---|
options optional | AsyncLocalStorageOptions<T> | Optional defaultValue and name. |
| Property | Type | Description | Since |
|---|---|---|---|
name readonly | string | Optional diagnostic name configured for this instance. | v0.0.0-alpha.2 |
AsyncLocalStorage.disable
Section titled “AsyncLocalStorage.disable”disable(): void;
Disable this AsyncLocalStorage instance and clear its current store binding.
voidAsyncLocalStorage.getStore
Section titled “AsyncLocalStorage.getStore”getStore(): T | undefined;
Return the current store value, or undefined (or defaultValue) outside run() / enterWith().
T | undefined — Current bound store value, or undefined.AsyncLocalStorage.run
Section titled “AsyncLocalStorage.run”run<R, TArgs extends any[] = any[]>(
store: T,
callback: (...args: TArgs) => R,
...args: TArgs
): R;
Run callback synchronously with store bound for all async operations
initiated within its dynamic extent.
| Name | Type | Description |
|---|---|---|
store | T | Context store value. |
callback | (...args: TArgs) => R | Function to invoke inside the store context. |
...args | TArgs | Additional arguments forwarded to callback. |
R — Return value of callback.AsyncLocalStorage.exit
Section titled “AsyncLocalStorage.exit”exit<R, TArgs extends any[] = any[]>(callback: (...args: TArgs) => R, ...args: TArgs): R;
Run callback synchronously outside the store context established by run() / enterWith().
| Name | Type | Description |
|---|---|---|
callback | (...args: TArgs) => R | Function to invoke without this store bound. |
...args | TArgs | Additional arguments forwarded to callback. |
R — Return value of callback.AsyncLocalStorage.enterWith
Section titled “AsyncLocalStorage.enterWith”enterWith(store: T): void;
Bind store into the remainder of the current synchronous execution turn.
| Name | Type | Description |
|---|---|---|
store | T | Context store value. |
voidAsyncLocalStorage.bind
Section titled “AsyncLocalStorage.bind”static bind<Func extends (...args: any[]) => any>(fn: Func): Func;
Bind fn to the currently active AsyncLocalStorage execution context.
| Name | Type | Description |
|---|---|---|
fn | Func | Function to wrap. |
Func — Wrapped function preserving the captured context.AsyncLocalStorage.snapshot
Section titled “AsyncLocalStorage.snapshot”static snapshot(): <R, TArgs extends any[]>(
fn: (...args: TArgs) => R,
...args: TArgs
) => R;
Capture the current async context and return a runner that executes callbacks inside that captured snapshot.
<R, TArgs extends any[]>(
fn: (...args: TArgs) => R,
...args: TArgs
) => R — Snapshot runner (fn, ...args) => result.AsyncResource
Section titled “AsyncResource”export class AsyncResource {
constructor(type: string, triggerAsyncId?: number | AsyncResourceOptions);
static bind<Func extends (...args: any[]) => any, ThisArg = unknown>(
fn: Func,
type?: string,
thisArg?: ThisArg,
): Func;
bind<Func extends (...args: any[]) => any>(fn: Func): Func;
runInAsyncScope<This, Result, TArgs extends any[] = any[]>(
fn: (this: This, ...args: TArgs) => Result,
thisArg?: This,
...args: TArgs
): Result;
emitDestroy(): this;
asyncId(): number;
triggerAsyncId(): number;
}
Explicit async resource wrapper used to preserve async context across custom queueing or callback boundaries.
constructor(type: string, triggerAsyncId?: number | AsyncResourceOptions);
Construct an AsyncResource.
| Name | Type | Description |
|---|---|---|
type | string | Resource category label (for example "HTTPRequest"). |
triggerAsyncId optional | number | AsyncResourceOptions | Optional triggering async ID or AsyncResourceOptions. |
AsyncResource.bind
Section titled “AsyncResource.bind”static bind<Func extends (...args: any[]) => any, ThisArg = unknown>(
fn: Func,
type?: string,
thisArg?: ThisArg,
): Func;
Bind fn to an anonymous or named AsyncResource captured at bind time.
| Name | Type | Description |
|---|---|---|
fn | Func | Function to bind. |
type optional | string | Optional resource type name. |
thisArg optional | ThisArg | Optional this binding for fn. |
Func — Context-bound function wrapper.AsyncResource.bind
Section titled “AsyncResource.bind”bind<Func extends (...args: any[]) => any>(fn: Func): Func;
Bind fn to this AsyncResource’s captured async execution context.
| Name | Type | Description |
|---|---|---|
fn | Func | Function to bind. |
Func — Context-bound function wrapper.AsyncResource.runInAsyncScope
Section titled “AsyncResource.runInAsyncScope”runInAsyncScope<This, Result, TArgs extends any[] = any[]>(
fn: (this: This, ...args: TArgs) => Result,
thisArg?: This,
...args: TArgs
): Result;
Execute fn within the async context of this AsyncResource.
| Name | Type | Description |
|---|---|---|
fn | (this: This, ...args: TArgs) => Result | Function to execute. |
thisArg optional | This | Optional this binding for fn. |
...args | TArgs | Positional arguments forwarded to fn. |
Result — Return value of fn.AsyncResource.emitDestroy
Section titled “AsyncResource.emitDestroy”emitDestroy(): this;
Explicitly signal that this async resource has been destroyed.
this — this for chaining.AsyncResource.asyncId
Section titled “AsyncResource.asyncId”asyncId(): number;
Return the unique async ID assigned to this AsyncResource.
number — Numeric async ID.AsyncResource.triggerAsyncId
Section titled “AsyncResource.triggerAsyncId”triggerAsyncId(): number;
Return the async ID of the context that triggered creation of this resource.
number — Triggering async ID.Interfaces
Section titled “Interfaces”AsyncLocalStorageOptions
Section titled “AsyncLocalStorageOptions”export interface AsyncLocalStorageOptions<T = unknown> {
defaultValue?: T;
name?: string;
}
Options accepted by new AsyncLocalStorage(options).
| Property | Type | Description | Since |
|---|---|---|---|
defaultValue optional | T | Default value returned by getStore() when no store is active. | v0.0.0-alpha.2 |
name optional | string | Optional diagnostic name for this AsyncLocalStorage instance. | v0.0.0-alpha.2 |
AsyncResourceOptions
Section titled “AsyncResourceOptions”export interface AsyncResourceOptions {
triggerAsyncId?: number;
requireManualDestroy?: boolean;
}
Options accepted by new AsyncResource(type, options).
| Property | Type | Description | Since |
|---|---|---|---|
triggerAsyncId optional | number | Async execution ID of the triggering resource (default executionAsyncId()). | v0.0.0-alpha.2 |
requireManualDestroy optional | boolean | Disable automatic destroy hook emission on GC when true. | v0.0.0-alpha.2 |
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** `AsyncLocalStorage` constructor. @since 0.0.0-alpha.2 */
AsyncLocalStorage: typeof AsyncLocalStorage;
/** `AsyncResource` constructor. @since 0.0.0-alpha.2 */
AsyncResource: typeof AsyncResource;
/** Return current execution async ID. @since 0.0.0-alpha.2 */
executionAsyncId: typeof executionAsyncId;
/** Return triggering async ID. @since 0.0.0-alpha.2 */
triggerAsyncId: typeof triggerAsyncId;
};
export default _default;
Default export object providing the node:async_hooks API.
