abortcontroller-polyfill
abortcontroller-polyfill copied to clipboard
Support AbortSignal.timeout
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.
Important thing, because it's feature breaks safari < 16
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 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
@mo @elhardoum Another static method AbortSignal.any() comes out:
- https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static
- microsoft/TypeScript#58026