Sub topic 3
Reading a Signal: aborted and reason
This chapter assumes you can create an AbortController and call abort() on it. It also assumes you know promises and try/catch. Here you will learn the two properties every cancellable function reads first: signal.aborted and signal.reason.
Two properties, one state change
An AbortSignal has two states. It starts out not aborted. When someone calls controller.abort(), it becomes aborted and stays that way. A signal cannot be reset. If you need to cancel again later, you need a new controller.
signal.abortedis a boolean. It isfalseuntil the controller aborts, thentrueforever.signal.reasonisundefineduntil the abort. After it,reasonholds the value passed toabort(). If no value was passed, it holds aDOMExceptionnamed"AbortError".
Watching the state change
Run this in Node 17.3 or later, or in any current browser console:
const controller = new AbortController();
const { signal } = controller;
console.log(signal.aborted); // false
console.log(signal.reason); // undefined
controller.abort();
console.log(signal.aborted); // true
console.log(signal.reason.name); // "AbortError"
console.log(signal.reason instanceof DOMException); // true
controller.abort(new Error('second call'));
console.log(signal.reason.name); // still "AbortError"The last two lines show that the first abort wins. Later calls to abort() do nothing, and the reason does not change.
Using the state inside a function
A cancellable function reads these properties to decide what to do. When aborted is true, the function should stop its work and reject (or throw) with signal.reason. Throwing signal.reason itself, instead of a new error you make up, lets callers see exactly why the work stopped. That reason could be the default AbortError, a TimeoutError, or a custom value the caller supplied.
function processItems(items, { signal } = {}) {
const done = [];
for (const item of items) {
if (signal?.aborted) {
throw signal.reason;
}
done.push(item * 2);
}
return done;
}
const controller = new AbortController();
console.log(processItems([1, 2, 3], { signal: controller.signal })); // [2, 4, 6]
controller.abort();
try {
processItems([1, 2, 3], { signal: controller.signal });
} catch (err) {
console.log(err.name); // "AbortError"
}The optional chaining in signal?.aborted lets callers leave out the signal entirely. Later chapters cover that convention and the shorter signal.throwIfAborted() helper.
Common mistakes
| Mistake | Why it fails | Fix |
|---|---|---|
| Checking `signal.reason` to test for abort | A caller can call `abort(undefined)`, which still stores the default `AbortError`, but other code may treat a falsy reason as "not aborted" | Test `signal.aborted` and read `reason` only after that |
| Throwing `new Error('aborted')` | Callers lose the original reason and cannot tell a timeout from user cancellation | Throw or reject with `signal.reason` |
| Reusing a controller after abort | The signal stays aborted, so every new call fails at once | Create a new `AbortController` for each operation |
Exercise
You run const c = new AbortController(); c.abort('stop'); c.abort('again');. What are c.signal.aborted and c.signal.reason?
Show answer
c.signal.aborted is true and c.signal.reason is the string "stop". The first call to abort() sets the reason, and the second call is ignored.
Exercise
Why should a cancellable function throw signal.reason instead of creating its own Error?
Show answer
signal.reason is the value the caller or the platform supplied. It may be an AbortError, a TimeoutError, or a custom error. Passing it through unchanged lets the caller tell these cases apart and handle each one correctly.