Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท node:sqlite and Runtime Services

Node.js executionAsyncId() function

Reference for executionAsyncId(), triggerAsyncId() and executionAsyncResource() in node:async_hooks: ids that identify the current async call and its cause.

executionAsyncId() returns the numeric id of the asynchronous context you are running in right now. Every asynchronous operation (a timer callback, a promise reaction, a socket event) has an id, and code running because of it reports that id. Its sibling triggerAsyncId() returns the id of the context that caused the current one to be created.

Together they let you reconstruct the chain of causes behind a piece of code, such as "this callback ran because that timer was set by that request handler". They are part of node:async_hooks, and unlike hooks, they are cheap and stable.

NODE
import { executionAsyncId, triggerAsyncId } from 'node:async_hooks';

console.log(executionAsyncId(), triggerAsyncId());

Try it

Node.js Following the chain of causes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Async functions and executionAsyncResource()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
executionAsyncId(): number
triggerAsyncId(): number
executionAsyncResource(): object

These functions take no parameters.

Function Description
executionAsyncId() The id of the current execution context.
triggerAsyncId() The id of the context that caused the current one to be created.
executionAsyncResource() The object representing the current resource (for example a Timeout), or an empty-like top-level object when no resource is active.

Returns: numbers for the ids; an object for the resource. At the very top level of a script the execution id is a small positive number (the bootstrap context); 0 means no async context is active.

Usage notes

  • Ids are only meaningful within one process run. Do not print them, store them, or assume specific values; compare them with each other.
  • Not a request id. An id identifies a single resource (one timer, one promise). Unrelated callbacks of one request have different ids. To carry a request through code, use AsyncLocalStorage.
  • Callbacks get their own ids; await is subtler. A timer or immediate callback runs under the id of that timer or immediate. Continuations after an await are promise reactions, and their ids are only distinct when promise tracking is on (see the last note).
  • Repeating resources reuse their id, as with setInterval(): each callback has the same execution id.
  • executionAsyncResource() can be used as a key in a WeakMap to attach data to the current resource, though AsyncLocalStorage is the supported way to do that.
  • Cheap to call. Unlike createHook(), calling these functions does not turn on tracking for everything.
  • Promise tracking: promise contexts are only tracked reliably when at least one hook is enabled or AsyncLocalStorage is in use. Without them, ids inside promise reactions may appear as the id of the surrounding context.