Skip to content

Instantly share code, notes, and snippets.

@assertnotnull
Created September 24, 2020 02:39
Show Gist options
  • Save assertnotnull/140b30f32f3f7931154f0147d97704ee to your computer and use it in GitHub Desktop.
Save assertnotnull/140b30f32f3f7931154f0147d97704ee to your computer and use it in GitHub Desktop.
reusable asyncstore
import { AsyncLocalStorage } from 'async_hooks';
const context = new AsyncLocalStorage;
export function getStore() {
return context;
}
export function run(cb) {
context.run(new Map(), cb)
}
export function set(k, v): void {
context.getStore().set(k, v)
}
export function get(k): object {
return context.getStore().get(k)
}
export function runWithRequest(req, res, cb) {
run(() => {
set('req', req)
set('res', res)
cb()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment