Skip to the content

Overview

What the library does, the problem that it solves, and the parts of this documentation.

What the library does

async-browser-context keeps a value for each asynchronous operation in a browser. The value is the context of the operation. For example, the context of a click handler can contain a request ID. Each function that the handler starts can read the request ID. It is not necessary to give the ID to each function as a parameter.

The library gives two APIs for the same mechanism:

  • The AsyncLocalStorage class, with the API and the behavior of the class in node:async_hooks of Node.js.
  • AsyncContext.Variable and AsyncContext.Snapshot of the TC39 AsyncContext proposal.

On Node.js, the package gives the native AsyncLocalStorage. The browser runtime and the Babel transform are not necessary there.

The problem

Node.js keeps the context with hooks in its engine. A browser has no such hooks. Also, JavaScript code cannot know when a native await continues: await does not use Promise.prototype.then. Thus, a library that only patches then loses the context after each await.

The library solves the problem in two parts:

  1. A Babel preset, or the Vite plugin, changes each async function into a generator function. A small function of the runtime, the coroutine, starts each step of the generator in the context of the async function. After the step, the coroutine sets the previous context again.
  2. The runtime patches Promise.prototype.then, the timers, the event listeners, the observers and other browser APIs. Each callback gets the context of the code that registered it.
Show the diagram source
flowchart TB
    source["Your code with async and await"] --> transform["Babel preset or Vite plugin"]
    transform --> output["Generators and coroutine()"]
    output --> runtime["Runtime: coroutine, bindGenerator, patches"]
    runtime --> value["get() gives the value of the current context"]
The build transforms the code. The runtime keeps the context while the code operates.

The parts of this documentation

PageWhat it tells
Get startedHow to install the library and configure Vite, Babel, webpack or Node.js.
Contexts and framesThe words of this documentation: context, root context, frame and registration.
Context rulesThe 13 rules that the library obeys, with an example for each rule.
The transformWhat the Babel preset does to async functions, for await loops and generators.
Patched APIsEach browser API that the runtime patches.
BoundariesThe guarantee of the library, and the tools that keep the context where your code meets code without the transform.
API referenceEach class, method and option.
GuidesRequest tracing, logging, error reports, React and tests.
DesignThe problems of the old design, and the reasons for the new design.

The Explore section has interactive pages. They start the real library in this page.

async-browser-context: AsyncLocalStorage and the TC39 AsyncContext API for browsers. The source code is on GitHub.

To change a page, edit its file in site/content/. The writing style guide tells you how.

An AI model (Claude, from Anthropic) wrote most of the text and the code of this site and of the library, under the direction of the author. The tests and the STE linter examine them.