AbortSignal: throwIfAborted() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2022.

Note: This feature is available in Web Workers.

The throwIfAborted() method throws the signal's abort reason if the signal has been aborted; otherwise it does nothing.

An API that needs to support aborting can accept an AbortSignal object and use throwIfAborted() to test and throw when the abort event is signalled.

This method can also be used to abort operations at particular points in code, rather than passing to functions that take a signal.

Syntax

js
throwIfAborted()

Parameters

None.

Return value

None (undefined).

Examples

The examples below come from the specification.

Aborting a polling operation

This example demonstrates how you can use throwIfAborted() to abort a polling operation.

Consider an asynchronous waitForCondition() function that is called with another asynchronous function func, a target value targetValue, and an AbortSignal. The method compares the result of func with targetValue in a loop, returning when they match.

js
async function waitForCondition(func, targetValue, { signal } = {}) {
  while (true) {
    signal?.throwIfAborted();

    const result = await func();
    if (result === targetValue) {
      return;
    }
  }
}

On each iteration of the loop, we use throwIfAborted() to throw the signal's reason if the operation has been aborted (and otherwise do nothing). If the signal is aborted, this will cause the waitForCondition() promise to be rejected.

Specifications

Specification
DOM
# ref-for-dom-abortsignal-throwifaborted①

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
throwIfAborted
Chrome – Full support
Chrome 100 (Release date: 2022-03-29)
footnote Full support
Edge – Full support
Edge 100 (Release date: 2022-04-01)
footnote Full support
Firefox – Full support
Firefox 97 (Release date: 2022-02-08)
footnote Full support
Opera – Full support
Opera 86 (Release date: 2022-04-20)
footnote Full support
Safari – Full support
Safari 15.4 (Release date: 2022-03-14)
footnote Full support
Chrome Android – Full support
Chrome Android 100 (Release date: 2022-03-29)
footnote Full support
Firefox for Android – Full support
Firefox for Android 97 (Release date: 2022-02-08)
footnote Full support
Opera Android – Full support
Opera Android 69 (Release date: 2022-05-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15.4 (Release date: 2022-03-14)
footnote Full support
Samsung Internet – Full support
Samsung Internet 19 (Release date: 2022-11-01)
footnote Full support
WebView Android – Full support
WebView Android 100 (Release date: 2022-03-29)
footnote Full support
WebView on iOS – Full support
WebView on iOS 15.4 (Release date: 2022-03-14)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1.17 (Release date: 2021-12-16)
footnote Full support
Node.js – No support
Node.js 16.17 – 16.17 (Release date: 2022-08-16)
footnote Removed in 17 and later
Node.js – Full support
Node.js 17.3 (Release date: 2021-12-17)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Has more compatibility info.

See also