Dev Helpers
← Back

Bind a callback to its context

Bind a callback to its context using Node.js.

Open this example in the interactive library

Source code

import {
  AsyncLocalStorage,
  AsyncResource,
  executionAsyncId,
} from "node:async_hooks";
const context = new AsyncLocalStorage();
console.log(
  (() => {
    const read = context.run("bound", () =>
      AsyncLocalStorage.bind(() => context.getStore()),
    );
    return read();
  })(),
);

Sample output

bound