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
- id: C1 claim: AsyncLocalStorage stores stay coherent through asynchronous operations. source: S1 evidence: "stores that stay coherent through asynchronous operations"
- id: C2 claim: getStore returns undefined outside a run or enterWith context. source: S1 evidence: "it returns undefined"
- id: C3 claim: run runs a function in a context and returns its return value. source: S2 evidence: "Runs a function synchronously within a context and returns its"
- id: C4 claim: run should be preferred over enterWith in ordinary use. source: S2 evidence: "
run()should be preferred overenterWith()"
Sources
- [S1] Asynchronous context tracking. Node.js. https://nodejs.org/api/async_context.html . License: MIT. Accessed 2026-11-17.
- [S2] async_context.md (Node.js source docs). nodejs/node (GitHub). https://github.com/nodejs/node/blob/main/doc/api/async_context.md . License: MIT. Accessed 2026-11-17.
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
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.},
}
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.