Dev Helpers
← Back

Capture context with snapshot

Capture context with snapshot using Node.js.

Open this example in the interactive library

Source code

import {
  AsyncLocalStorage,
  AsyncResource,
  executionAsyncId,
} from "node:async_hooks";
const context = new AsyncLocalStorage();
console.log(
  (() => {
    const run = context.run("saved", () => AsyncLocalStorage.snapshot());
    return run(() => context.getStore());
  })(),
);

Sample output

saved