← All notes

AsyncLocalStorage keeps a store coherent across async operations

Answer: AsyncLocalStorage creates stores that stay coherent through asynchronous operations. Import it from node:async_hooks. run runs a function synchronously within a context and returns its return value. The store is accessible to any asynchronous operations created within the callback. getStore() returns the current store.

run and getStore

run executes a function synchronously in the context and returns its return value. The store is not visible outside that callback, but it is visible to asynchronous operations created inside the callback. If the callback throws, run throws too.

getStore() returns the current store. Outside a context started with run() or enterWith(), it returns undefined. Each AsyncLocalStorage instance keeps an independent storage context.

Prefer run over enterWith

enterWith(store) moves into the context for the rest of the current synchronous execution, then keeps the store for asynchronous calls that follow. Subsequent event handlers also run within that context when it is entered within an event handler. Prefer run() over enterWith() unless you have strong reasons to use the latter method.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Node.js, Asynchronous context tracking, MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors; nodejs/node (GitHub), async_context.md (Node.js source docs), MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Node.js, Asynchronous context tracking, MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors; nodejs/node (GitHub), async_context.md (Node.js source docs), MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors.

Cite this

Permanent ID: iil:note:wzd8q9tx

iinventedlight. 2026. "AsyncLocalStorage keeps a store coherent across async operations." iinventedlight Library, iil:note:wzd8q9tx, version of 2026-11-17. https://iinventedlight.com/library/notes/node-async-local-storage/. License: CC-BY-4.0.

@misc{iil_note_wzd8q9tx,
  author = {{iinventedlight}},
  title = {{AsyncLocalStorage keeps a store coherent across async operations}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-17},
  url = {https://iinventedlight.com/library/notes/node-async-local-storage/},
  note = {Permanent ID iil:note:wzd8q9tx. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/node-async-local-storage.md.