Skip to content

Instantly share code, notes, and snippets.

@3p3r
Created March 19, 2026 00:58
Show Gist options
  • Select an option

  • Save 3p3r/540ed2321443a04f44d5165ecfb04ec9 to your computer and use it in GitHub Desktop.

Select an option

Save 3p3r/540ed2321443a04f44d5165ecfb04ec9 to your computer and use it in GitHub Desktop.
AsyncLocalStorage mock
export class AsyncLocalStorage<T = any> {
private store: T | undefined;
getStore(): T | undefined {
return this.store;
}
run<R>(store: T, callback: (...args: any[]) => R, ...args: any[]): R {
const previousStore = this.store;
this.store = store;
try {
return callback(...args);
} finally {
this.store = previousStore;
}
}
enterWith(store: T): void {
this.store = store;
}
exit<R>(callback: (...args: any[]) => R, ...args: any[]): R {
const previousStore = this.store;
this.store = undefined;
try {
return callback(...args);
} finally {
this.store = previousStore;
}
}
disable(): void {
this.store = undefined;
}
}
export default { AsyncLocalStorage };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment