Skip to main content

Class: AsyncLocalStorage<T>

workflow.AsyncLocalStorage

Workflow isolate (mostly compatible) port of Node's AsyncLocalStorage.

IMPORTANT Works for Promises (and async functions) only, not for callbacks.

example

const storage = new AsyncLocalStorage<number>();
storage.run(1, async () => {  storage.getStore(); // 1  await sleep(20);  storage.getStore(); // 1  setTimeout(() => {    storage.getStore(); // undefined  }, 20);});

Type parameters#

Name
T

Constructors#

constructor#

new AsyncLocalStorage<T>()

Type parameters#

Name
T

Methods#

disable#

disable(): void

Disables the instance of AsyncLocalStorage. Subsequent calls to asyncLocalStorage.getStore() might return undefined.

Calling asyncLocalStorage.disable() is required before the asyncLocalStorage can be garbage collected. This does not apply to stores provided by the asyncLocalStorage, as those objects are garbage collected along with the corresponding async resources.

Use this method when the asyncLocalStorage is not in use anymore in the current process.

Returns#

void


getStore#

getStore(): undefined | T

Returns the current store.

If called outside of an asynchronous context initialized by calling asyncLocalStorage.run() it returns undefined.

Returns#

undefined | T


run#

run<A, R>(store, callback, ...args): R

Runs a function synchronously within a context and returns its return value. The store is not accessible outside of the callback function. The store is accessible to any asynchronous operations created within the callback.

The optional args are passed to the callback function.

Type parameters#

NameType
Aextends any[]
RR

Parameters#

NameType
storeT
callback(...args: A) => R
...argsA

Returns#

R