AbortSignal.timeout()
The AbortSignal.timeout() static method returns an AbortSignal that will automatically abort after a specified time.
The signal aborts with a TimeoutError DOMException on timeout.
The timeout is based on active rather than elapsed time, and will effectively be paused if the code is running in a suspended worker, or while the document is in a back-forward cache ("bfcache").
To combine multiple signals, you can use AbortSignal.any(), for example, to directly abort a download using either a timeout signal or by calling AbortController.abort().
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 124 | 124 | 100 | 16 | 124 | 16 | |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Notes 3 item(s)
Limitation
- This browser only partially implements this feature
Removed
- This feature was removed in a later browser version (124)
Implementation note
- Always aborts with an `AbortError` on timeout, not a `TimeoutError`.
Notes 3 item(s)
Limitation
- This browser only partially implements this feature
Removed
- This feature was removed in a later browser version (124)
Implementation note
- Always aborts with an `AbortError` on timeout, not a `TimeoutError`.
Notes 3 item(s)
Limitation
- This browser only partially implements this feature
Removed
- This feature was removed in a later browser version (124)
Implementation note
- Always aborts with an `AbortError` on timeout, not a `TimeoutError`.
Syntax
JAVASCRIPT
const url = "https://path_to_large_file.mp4";
try {
const res = await fetch(url, { signal: AbortSignal.timeout(5000) });
const result = await res.blob();
// …
} catch (err) {
if (err.name === "TimeoutError") {
// This exception is from the abort signal
console.error("Timeout: It took more than 5 seconds to get the result!");
} else if (err.name === "AbortError") {
// This exception is from the fetch itself
console.error(
"Fetch aborted by user action (browser stop button, closing tab, etc.",
);
} else if (err.name === "TypeError") {
console.error("AbortSignal.timeout() method is not supported");
} else {
// A network error, or some other problem.
console.error(`Error: type: ${err.name}, message: ${err.message}`);
}
} Use cases
-
Using AbortSignal.timeout()
The AbortSignal.
Cautions
- May not be supported in older browsers.
Implementation notes
- Some APIs require a secure context (HTTPS) or user activation. Check the requirements before use.