Skip to main content
AsyncLocalStorage.prototype.exit - async_hooks - Node documentation
method AsyncLocalStorage.prototype.exit
Unstable

Usage in Deno

import { AsyncLocalStorage } from "node:async_hooks";
AsyncLocalStorage.prototype.exit<
R,
TArgs extends any[],
>
(
callback: (...args: TArgs) => R,
...args: TArgs,
): R

Runs a function synchronously outside of a context and returns its return value. The store is not accessible within the callback function or the asynchronous operations created within the callback. Any getStore() call done within the callback function will always return undefined.

The optional args are passed to the callback function.

If the callback function throws an error, the error is thrown by exit() too. The stacktrace is not impacted by this call and the context is re-entered.

Example:

// Within a call to run
try {
  asyncLocalStorage.getStore(); // Returns the store object or value
  asyncLocalStorage.exit(() => {
    asyncLocalStorage.getStore(); // Returns undefined
    throw new Error();
  });
} catch (e) {
  asyncLocalStorage.getStore(); // Returns the same object or value
  // The error will be caught here
}

Type Parameters

R
TArgs extends any[]

Parameters

callback: (...args: TArgs) => R
...args: TArgs

Return Type

R