Dev Helpers
← Back

Separate concurrent contexts

Separate concurrent contexts 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(
  await Promise.all(
    ["a", "b"].map((id) =>
      context.run(id, async () => {
        await Promise.resolve();
        return context.getStore();
      }),
    ),
  ),
);

Sample output

[ 'a', 'b' ]