interface
async_hooks.RunScope
interface RunScope
A disposable scope returned by asyncLocalStorage.withScope() that automatically restores the previous store value when disposed. This class implements the Explicit Resource Management protocol and is designed to work with JavaScript's using syntax.
The scope automatically restores the previous store value when the using block exits, whether through normal completion or by throwing an error.
Explicitly ends the scope and restores the previous store value. This method is idempotent: calling it multiple times has the same effect as calling it once.
The
[Symbol.dispose]()method defers todispose().If
withScope()is called without theusingkeyword,dispose()must be called manually to restore the previous store value. Forgetting to calldispose()will cause the store value to persist for the remainder of the current execution context:import { AsyncLocalStorage } from 'node:async_hooks'; const storage = new AsyncLocalStorage(); // Without using, the scope must be disposed manually const scope = storage.withScope('my-store'); // storage.getStore() === 'my-store' here scope.dispose(); // Restore previous value // storage.getStore() === undefined here