Releases: ggoodman/context
v2.1.0
v2.0.0
v1.1.2
v1.1.1
Fixed
-
Fixed an illegal invocation when using the default implementation of the
setTimeoutandclearTimeoutfunctions.Previously, these functions were being called with the options object as the receiver instead of
globalThis. This change calls these functions in a way that their receivers will beglobalThis.
v1.1.0
Added
-
Added support for treating a
Contextas aPromiseLike<never>.This is useful, for example, when you want to use a
Contextwith a timeout to 'race' anotherPromise-returning operation, like an http request.(async () => { const { context } = Background.withTimeout(2000); const resPromise = fetch('https://foo.bar').then(res => res.json()); // This will throw an Error that will either be true for isCancellationError or isDeadlineExceededError. const res = await Promise.race([ context, resPromise ]); })();
-
Add support for converting a
Contextto anAbortSignalin environments that support these usingasAbortSignal.This feature allows easy interoperability with APIs that support
AbortSignals as a mechanism to propagate cancellation.For example, with
fetch, adding a timeout could be as easy as:(async () => { const { context } = Background.withTimeout(2000); const signal = asAbortSignal(context); const res = await fetch('https://foo.bar', { signal }); const data = await res.json(); })();