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(); })();