Sub topic 4
Reacting to the abort Event
This chapter assumes you know how addEventListener works, how closures capture variables, and the basics from the previous chapters: an AbortController owns a signal, and signal.aborted and signal.reason describe its state. Reading those properties tells you whether cancellation has happened at a given moment. The abort event tells you when it happens, so your code can react right away and doesn't need to poll.
The event fires once, during abort()
An AbortSignal is an EventTarget. When the controller's abort() method runs, the signal does three things in this order:
- It sets
signal.abortedtotrue. - It sets
signal.reason. With no argument, the reason is aDOMExceptionnamed"AbortError". - It dispatches a single
abortevent to its listeners.
const controller = new AbortController();
const { signal } = controller;
signal.addEventListener('abort', () => {
console.log('aborted:', signal.aborted); // true
console.log('reason:', signal.reason.name); // "AbortError"
}, { once: true });
controller.abort();The listener runs synchronously, inside the abort() call. When abort() returns, every listener has already finished. That is why the listener can read signal.aborted and signal.reason: both are already set when the event fires.
The event never fires twice, and never fires late
A signal can be aborted only once. A second call to abort() does nothing: the reason stays the same and no second event fires. Listeners also don't replay. If you attach a listener after the signal has already aborted, that listener never runs.
const controller = new AbortController();
controller.abort();
controller.signal.addEventListener('abort', () => {
console.log('this never prints');
});
controller.abort(); // no effect, the signal is already aborted
console.log(controller.signal.aborted); // trueThis is the most common mistake with the abort event. A function that only listens for the event will wait forever when it receives a signal that was cancelled before the call. The next chapters fix this by checking signal.aborted first and only then subscribing.
Choosing how to listen
| Approach | When to use it |
|---|---|
| `addEventListener('abort', fn, { once: true })` | The default choice. It allows several listeners, and you can remove one later. |
| `signal.onabort = fn` | Quick scripts. Assigning a new handler replaces the old one, so another piece of code can overwrite yours. |
Adding { once: true } is a good habit even though the event can only fire once. It documents your intent, and the browser or runtime drops its reference to the listener after the event fires.
Inside the listener, keep the work small and synchronous. Typical jobs are clearing a timer, closing a socket, or rejecting a promise with signal.reason. If a listener throws, the error is reported as an uncaught exception, but the other listeners still run and abort() does not throw back to the caller.
Exercise
The code calls controller.abort(), then signal.addEventListener('abort', () => console.log('A')), then controller.abort() again. What gets printed, and why?
Show answer
Nothing is printed. The abort event fires only once, during the first call to abort(), and no listener was attached yet. Listeners added later never run, and a second abort() call on an aborted signal has no effect.
Exercise
Inside an abort listener, the code logs signal.aborted and signal.reason. Can either value still be the "not aborted" state at that point?
Show answer
No. abort() sets aborted to true and assigns reason before it dispatches the event, so both values are final by the time any listener runs.