Skip to content

v1.1.0

Choose a tag to compare

@ggoodman ggoodman released this 08 Jun 17:00

Added

  • Added support for treating a Context as a PromiseLike<never>.

    This is useful, for example, when you want to use a Context with a timeout to 'race' another Promise-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 Context to an AbortSignal in environments that support these using asAbortSignal.

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