---
title: "AbortSignal throwIfAborted"
description: "If the signal has been aborted, throw the signal’s abort reason. Practical notes and code examples on AbortSignal in JS fast."
url: "https://h3manth.com/scribe/abortsignal-throwifaborted/"
canonical_url: "https://h3manth.com/scribe/abortsignal-throwifaborted/"
markdown_url: "https://h3manth.com/scribe/blog/abortsignal-throwifaborted.md"
publishDate: "2022-04-07T00:00:00.000Z"
author: "Hemanth HM"
category: "web"
tags: ["javascript","web","dom","abort"]
word_count: 241
reading_time_minutes: 2
x_markdown_tokens: 704
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# AbortSignal throwIfAborted

> If the signal has been aborted, throw the signal’s abort reason. Practical notes and code examples on AbortSignal in JS fast.

`signal.throwIfAborted()` API throws signal's abort reason, if signal's AbortController has signaled to abort; otherwise, does nothing.

## Quick Examples

```javascript
const signal = AbortSignal.abort(new Error("💣"))
/*
AbortSignal {
  aborted: true,
  reason: Error: 💣 at <anonymous>:1:28,
  onabort: null
}
*/

signal.throwIfAborted()
// Uncaught Error: 💣 at <anonymous>:1:28
```

```javascript
// From the issues
function animationFrame(abortSignal) {
  return new Promise((resolve, reject) => {
    abortSignal?.throwIfAborted();
    
    const frameRequest = requestAnimationFrame(time => resolve(time));
    
    abortSignal?.addEventListener("abort", () => {
      cancelAnimationFrame(frameRequest);
      reject(new AbortError());
    }, { once: true });
  });
}
```

```javascript
// From the spec
async function waitForCondition(func, targetValue, { signal } = {}) {
  while (true) {
    signal?.throwIfAborted();
    
    const result = await func();
    if (result === targetValue) {
      return;
    }
  }
}
```

Signal-handling methods can use `throwIfAborted` API to check a signal's abort status and propagate the abort reason, ex: async activities that might affect a signal's state.

## Browser Implementation

Below is the crux from [Firefox](https://bugzilla.mozilla.org/show_bug.cgi?id=1745372)'s implementation:

```cpp
// https://dom.spec.whatwg.org/#dom-abortsignal-throwifaborted
void AbortSignal::ThrowIfAborted(JSContext* aCx, ErrorResult& aRv) {
  aRv.MightThrowJSException();
  if (Aborted()) {
    JS::Rooted<JS::Value> reason(aCx);
    GetReason(aCx, &reason);
    aRv.ThrowJSException(aCx, reason);
  }
}
```

[Safari](https://bugs.webkit.org/show_bug.cgi?id=234127) is pretty similar:

```cpp
void AbortSignal::throwIfAborted(JSC::JSGlobalObject& lexicalGlobalObject)
{
  if (!aborted())
    return;
    
  auto& vm = lexicalGlobalObject.vm();
  auto scope = DECLARE_THROW_SCOPE(vm);
  
  throwException(&lexicalGlobalObject, scope, m_reason);
}
```

[Chrome](https://bugs.chromium.org/p/chromium/issues/detail?id=1273458):

```cpp
void AbortSignal::throwIfAborted(
    ScriptState* script_state,
    ExceptionState& exception_state) const {
  if (!aborted())
    return;
  exception_state.RethrowV8Exception(reason(script_state).V8Value());
}
```

## Summary

`throwIfAborted()` should:
- throw `abort.reason` if signal aborted
- throw primitive `abort.reason` if signal aborted  
- should not throw if signal not aborted
