Async context

AsyncLocalStorage.snapshot()

Captures the current execution context and returns a function that invokes another function in that captured context. Use it for controlled callback handoff, not to carry a stale request context into unrelated work. Tested on Node.js 22.16.0. Example output (one line per entry): ["captured current"].

Formal Syntax & Structure

nodejs syntaxnodejs
AsyncLocalStorage.snapshot()

Return Value

Function

Context-restoring callback runner.

Working Example

Example Snippetnodejs
import { AsyncLocalStorage } from 'node:async_hooks';const als = new AsyncLocalStorage();try {    const run = als.run('captured', () => AsyncLocalStorage.snapshot());    als.run('current', () => console.log(run(() => als.getStore()), als.getStore()));}finally {    als.disable();}
Node.js Reference