node:async_hooks

AsyncLocalStorage, AsyncResource, and createHook, with Elide's context propagation rules.

node:async_hooks provides AsyncLocalStorage, AsyncResource, and createHook; import them from "node:async_hooks".

import { AsyncLocalStorage } from "node:async_hooks";

const storage = new AsyncLocalStorage();
storage.run("request", async () => {
  await null;
  console.log(storage.getStore()); // request
  queueMicrotask(() => console.log(storage.getStore())); // undefined
});

Differences from Node

  • AsyncLocalStorage context is lost in queueMicrotask callbacks and node:fs callbacks.
  • createHook callbacks (init, before, after, destroy) fire for timers, immediates, and Promise continuations.