Skip to the content

Patched APIs

Each browser API that the runtime patches, and the context that its callbacks get.

The runtime patches these APIs when it loads. It patches each API one time for each global object, also when the page loads two copies of the library. Each patched function keeps its name, its length and its other properties. Function.prototype.toString shows the source text of the original function.

Promises

APICallback context
Promise.prototype.thenThe context of the code that uses then() (rule C4).
catch, finally, Promise.all, race, allSettled, anyThese functions use then(), so they get the same rule.

The runtime does not replace the Promise constructor (rule C11).

Timers and schedulers

APICallback context
setTimeout, setInterval, setImmediateThe context of the code that scheduled the callback (rule C6).
queueMicrotaskThe same.
requestAnimationFrame, requestIdleCallbackThe same.
Scheduler.prototype.postTaskThe same.
HTMLVideoElement.prototype.requestVideoFrameCallbackThe same.
XRSession.prototype.requestAnimationFrameThe same.

A setTimeout callback in text form does not change.

Events

APICallback context
EventTarget.prototype.addEventListener and removeEventListenerRule C13: the context of the dispatch, or the registration context.
Each on... property of each EventTarget class and of windowThe same. The getter gives the original handler.
MediaQueryList.prototype.addListener and removeListenerThe same.

The patch keeps the rules of the DOM: a second registration of the same listener has no effect, and once and signal remove the listener.

Observers and registries

These constructors get a callback. The callback gets the context of the construction. The runtime replaces each constructor with a Proxy, so instanceof, subclasses and static members do not change.

ConstructorCallback context
MutationObserver, ResizeObserver, IntersectionObserverThe context of the construction.
PerformanceObserver, ReportingObserver, PressureObserverThe same.
FinalizationRegistryThe same. The TC39 draft specifies this rule.

Streams

The constructors ReadableStream, WritableStream and TransformStream get an object with methods. The stream uses these methods later, from its own queue. Each method gets the context of the construction.

ConstructorMethods
ReadableStreamstart, pull, cancel
WritableStreamstart, write, close, abort
TransformStreamstart, transform, flush, cancel

Other callback APIs

Each function argument of these methods gets the context of the method use.

APIMethods
GeolocationgetCurrentPosition, watchPosition
LockManagerrequest
HTMLCanvasElementtoBlob
DataTransferItemgetAsString
FileSystemDirectoryReader, FileSystemFileEntry, FileSystemDirectoryEntry, FileSystemEntryreadEntries, file, getFile, getDirectory, getParent, getMetadata
BaseAudioContextdecodeAudioData
MediaSessionsetActionHandler
RTCPeerConnectionThe old callback forms of createOffer, createAnswer, setLocalDescription, setRemoteDescription, addIceCandidate and getStats
NotificationrequestPermission
ArrayfromAsync
DocumentstartViewTransition, also with an object that has an update callback
NavigateEventintercept, for the handler callback

APIs that the runtime does not patch

The library does not patch other callback APIs. For a callback of such an API, use AsyncContext.Snapshot.wrap(callback) or AsyncLocalStorage.bind(callback). The boundaries page tells more.

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.