Links
- https://medium.com/wix-engineering/solving-the-async-context-challenge-in-node-js-088864aa715e
- https://nodejs.org/api/async_context.html
// asyncLocalStorage.ts
import { AsyncLocalStorage } from 'node:async_hooks';
export const asyncLocalStorage = new AsyncLocalStorage<Map<string, string>>()
// upper level
const store = new Map<string, string>();
store.set("traceId", "someTraceId");
asyncLocalStorage.run(store, async () => {
next();
})
// lower level (e.x. Logger)
const store = asyncLocalStorage.getStore();
console.log("some log", store?.get("traceId"));