Ergonomic typed context for async flows using AsyncLocalStorage
npm install context-localimport createContext from 'context-local';
const userContext = createContext({name: 'anonymous'});
userContext.run({name: 'Alice'}, () => {
console.log(userContext.get());
//=> {name: 'Alice'}
});
console.log(userContext.get());
//=> {name: 'anonymous'}Returns a context object with run, get, and runWith methods.
Type: T
The default value returned by .get() when outside a .run() scope.
Runs function_ with value in the async context. Returns the result of function_.
Returns the current context value or the default value.
Returns a curried function (fn) => context.run(value, fn).
- node:async_hooks - Node.js AsyncLocalStorage
MIT