abortcontroller-polyfill icon indicating copy to clipboard operation
abortcontroller-polyfill copied to clipboard

Support AbortSignal.timeout

Open johngeorgewright opened this issue 2 years ago • 4 comments

See https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout

The static AbortSignal.timeout() method returns an AbortSignal that will automatically abort after a specified time.

johngeorgewright avatar Feb 11 '23 23:02 johngeorgewright

Important thing, because it's feature breaks safari < 16

ccxdev avatar Feb 21 '23 15:02 ccxdev

Here's a temporary fix:

// safari polyfill
if ("AbortSignal" in window) {
  AbortSignal.timeout = AbortSignal.timeout || ((duration) =>
  {
    const controller = new AbortController;
    setTimeout(() => controller.abort(), duration);
    return controller.signal;
  });
}

elhardoum avatar May 10 '23 01:05 elhardoum

@elhardoum This cannot work because your setTimeout will start when AbortSignal.timeout() is called instead of the request itself. It also required a clearTimeout if the request finished earlier.

@nekotoriy, not only Safari < 16: https://caniuse.com/mdn-api_abortsignal_timeout_static

DevSide avatar Aug 01 '23 14:08 DevSide

@mo @elhardoum Another static method AbortSignal.any() comes out:

  1. https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static
  2. microsoft/TypeScript#58026

TechQuery avatar Apr 30 '24 14:04 TechQuery