Sub topic 11
Abort Reasons: AbortError, TimeoutError, and Custom Reasons
This chapter assumes you already know how to create an AbortController, read signal.aborted, and reject a promise with signal.reason. Here we look at what that reason actually is, because callers rely on it to decide what happened.
What signal.reason holds
Every aborted signal carries a reason. The reason is the value your function should reject with or throw, and it comes from one of three places:
| How the signal aborted | Value of `signal.reason` | `reason.name` |
|---|---|---|
| `controller.abort()` with no argument | `DOMException` | `"AbortError"` |
| `AbortSignal.timeout(ms)` expired | `DOMException` | `"TimeoutError"` |
| `controller.abort(value)` | `value`, exactly as passed | whatever `value` has |
Before a signal aborts, signal.reason is undefined. After it aborts, the reason never changes. Calling abort() a second time has no effect.
Custom reasons
You can pass any value to abort(). That value then comes back through both signal.reason and throwIfAborted():
const controller = new AbortController();
controller.abort(new Error('User navigated away'));
console.log(controller.signal.reason.message); // "User navigated away"
try {
controller.signal.throwIfAborted();
} catch (err) {
console.log(err.message); // "User navigated away"
}A custom reason lets the code that cancels explain why it cancelled. Built-in APIs pass the reason through unchanged too. If you abort a fetch with a custom Error, the fetch promise rejects with that same Error object.
Why your function should reject with signal.reason
Inside a cancellable function, don't make up a new error. Reject with signal.reason:
// Good: preserves whatever the canceller intended
reject(signal.reason);
// Bad: throws away TimeoutError and custom reasons
reject(new Error('aborted'));If you reject with your own error, every abort looks the same to the caller. A timeout, a user clicking "Cancel", and a custom "navigated away" reason all become one generic message. Passing signal.reason through keeps your function consistent with fetch, setTimeout from timers/promises in Node, and the other platform APIs.
Checking the error type
The two built-in reasons are both DOMException instances, so instanceof can't tell them apart. Use name instead:
const timeoutSignal = AbortSignal.timeout(10);
setTimeout(() => {
const reason = timeoutSignal.reason;
console.log(reason instanceof DOMException); // true
console.log(reason.name); // "TimeoutError"
}, 50);
const manual = AbortSignal.abort();
console.log(manual.reason.name); // "AbortError"AbortSignal.abort(reason) is a static helper that returns a signal that is already aborted. It is useful for testing the fail-fast path in your functions. Later chapters show how callers branch on these names and how to set deadlines with AbortSignal.timeout().
Exercise
A function rejects with new Error('cancelled') whenever its signal aborts. A caller passes AbortSignal.timeout(2000) and checks err.name === 'TimeoutError' to show a "took too long" message. Why does the message never appear, and what one-line change fixes it?
Show answer
The function throws away the original reason, so the caller always gets a plain Error whose name is "Error". The fix is to reject with signal.reason instead of new Error('cancelled'). The caller then receives the DOMException named "TimeoutError".
Exercise
What does signal.reason hold after controller.abort('stop'), and what does signal.throwIfAborted() throw?
Show answer
signal.reason is the string "stop", and throwIfAborted() throws that same string. A reason can be any value, not only an Error.