Skip to content
VaelJS Blog

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"
Since v0.0.0-alpha.2 vjs (V8)
Cargo feature: node-compat (enabled by default in vjs; absent in vjs-lite; disable with --no-node-compat)
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);
functionSince v0.0.0-alpha.2vjs only
export function executionAsyncId(): number;

Return the unique numeric ID of the current async execution context.

Returns
number — Current execution async ID (0 when outside an async scope).
functionSince v0.0.0-alpha.2vjs only
export function triggerAsyncId(): number;

Return the async ID of the context that scheduled the current execution.

Returns
number — Triggering async ID.
classSince v0.0.0-alpha.2vjs only
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
constructor(options?: AsyncLocalStorageOptions<T>);

Construct a new AsyncLocalStorage instance.

Parameters
NameTypeDescription
options optionalAsyncLocalStorageOptions<T>Optional defaultValue and name.
Properties
PropertyTypeDescriptionSince
name readonlystringOptional diagnostic name configured for this instance.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
disable(): void;

Disable this AsyncLocalStorage instance and clear its current store binding.

Returns
void
methodSince v0.0.0-alpha.2
getStore(): T | undefined;

Return the current store value, or undefined (or defaultValue) outside run() / enterWith().

Returns
T | undefined — Current bound store value, or undefined.
methodSince v0.0.0-alpha.2
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.

Parameters
NameTypeDescription
storeTContext store value.
callback(...args: TArgs) => RFunction to invoke inside the store context.
...argsTArgsAdditional arguments forwarded to callback.
Returns
R — Return value of callback.
methodSince v0.0.0-alpha.2
exit<R, TArgs extends any[] = any[]>(callback: (...args: TArgs) => R, ...args: TArgs): R;

Run callback synchronously outside the store context established by run() / enterWith().

Parameters
NameTypeDescription
callback(...args: TArgs) => RFunction to invoke without this store bound.
...argsTArgsAdditional arguments forwarded to callback.
Returns
R — Return value of callback.
methodSince v0.0.0-alpha.2
enterWith(store: T): void;

Bind store into the remainder of the current synchronous execution turn.

Parameters
NameTypeDescription
storeTContext store value.
Returns
void
methodstaticSince v0.0.0-alpha.2
static bind<Func extends (...args: any[]) => any>(fn: Func): Func;

Bind fn to the currently active AsyncLocalStorage execution context.

Parameters
NameTypeDescription
fnFuncFunction to wrap.
Returns
Func — Wrapped function preserving the captured context.
methodstaticSince v0.0.0-alpha.2
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.

Returns
<R, TArgs extends any[]>( fn: (...args: TArgs) => R, ...args: TArgs ) => R — Snapshot runner (fn, ...args) => result.
classSince v0.0.0-alpha.2vjs only
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
constructor(type: string, triggerAsyncId?: number | AsyncResourceOptions);

Construct an AsyncResource.

Parameters
NameTypeDescription
typestringResource category label (for example "HTTPRequest").
triggerAsyncId optionalnumber | AsyncResourceOptionsOptional triggering async ID or AsyncResourceOptions.
Methods
methodstaticSince v0.0.0-alpha.2
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.

Parameters
NameTypeDescription
fnFuncFunction to bind.
type optionalstringOptional resource type name.
thisArg optionalThisArgOptional this binding for fn.
Returns
Func — Context-bound function wrapper.
methodSince v0.0.0-alpha.2
bind<Func extends (...args: any[]) => any>(fn: Func): Func;

Bind fn to this AsyncResource’s captured async execution context.

Parameters
NameTypeDescription
fnFuncFunction to bind.
Returns
Func — Context-bound function wrapper.
methodSince v0.0.0-alpha.2
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.

Parameters
NameTypeDescription
fn(this: This, ...args: TArgs) => ResultFunction to execute.
thisArg optionalThisOptional this binding for fn.
...argsTArgsPositional arguments forwarded to fn.
Returns
Result — Return value of fn.
methodSince v0.0.0-alpha.2
emitDestroy(): this;

Explicitly signal that this async resource has been destroyed.

Returns
this — this for chaining.
methodSince v0.0.0-alpha.2
asyncId(): number;

Return the unique async ID assigned to this AsyncResource.

Returns
number — Numeric async ID.
methodSince v0.0.0-alpha.2
triggerAsyncId(): number;

Return the async ID of the context that triggered creation of this resource.

Returns
number — Triggering async ID.
interfaceSince v0.0.0-alpha.2vjs only
export interface AsyncLocalStorageOptions<T = unknown> {
  defaultValue?: T;
  name?: string;
}

Options accepted by new AsyncLocalStorage(options).

Properties
PropertyTypeDescriptionSince
defaultValue optionalTDefault value returned by getStore() when no store is active.v0.0.0-alpha.2
name optionalstringOptional diagnostic name for this AsyncLocalStorage instance.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface AsyncResourceOptions {
  triggerAsyncId?: number;
  requireManualDestroy?: boolean;
}

Options accepted by new AsyncResource(type, options).

Properties
PropertyTypeDescriptionSince
triggerAsyncId optionalnumberAsync execution ID of the triggering resource (default executionAsyncId()).v0.0.0-alpha.2
requireManualDestroy optionalbooleanDisable automatic destroy hook emission on GC when true.v0.0.0-alpha.2
defaultSince v0.0.0-alpha.2vjs only
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.