On this page

C

RunScope

History
Stability: 1Experimental

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.

M

scope.dispose

History
scope.dispose(): void

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 to dispose().

If withScope() is called without the using keyword, dispose() must be called manually to restore the previous store value. Forgetting to call dispose() 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
const { AsyncLocalStorage } = require('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