Sub topic 2
AbortController and AbortSignal in One Minute
This chapter assumes you already know JavaScript functions and destructuring, promises with async/await, and the basics of addEventListener. It introduces the two objects that every later chapter builds on. Later chapters look closely at the signal's properties and its event.
Two objects, two roles
The cancellation API splits the work between two objects:
- An AbortController belongs to the code that wants to cancel something. It has one method that matters,
abort(). - An AbortSignal belongs to the code that does the work. You get it from
controller.signal. It reports whether cancellation has happened, and it fires an event when it does.
Because of this split, passing a signal to a function does not give that function the power to cancel anything. The signal can only be observed. Only whoever holds the controller can trigger the abort. Built-in APIs such as fetch follow the same model: you keep the controller and hand out the signal.
| Object | Who holds it | What it can do |
|---|---|---|
| `AbortController` | The caller | Call `abort()` to request cancellation |
| `AbortSignal` | The cancellable function | Report `aborted` and `reason`, fire `abort` |
A minimal example
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 example runs in any modern browser and in Node.js 17 or later. Here is what happens, in order:
new AbortController()creates a controller, and with it a fresh signal whoseabortedisfalse.- The listener subscribes to the signal's
abortevent. controller.abort()flips the signal to aborted. Because no reason was given, it stores aDOMExceptionnamed"AbortError"as the reason. Then it dispatches the event, which runs the listener synchronously.
Rules worth remembering
- Abort is one-way. Once a signal is aborted it stays aborted. You cannot reset it, so create a new controller for each new operation.
- Calling
abort()again does nothing. The event fires once, and the first reason is kept. - One signal can serve many consumers. You can pass the same signal to several functions or requests, and a single
abort()cancels all of them. - Aborting is a request, not a kill switch. The signal does not stop any code by itself. Your function has to notice the signal and stop. Most of this tutorial is about doing that properly.
Exercise
You call controller.abort() twice on the same controller, with an abort listener registered using { once: true }. How many times does the listener run, and what is signal.reason.name afterwards?
Show answer
The listener runs once. The second abort() call does nothing because the signal is already aborted. signal.reason.name is "AbortError", the default reason from the first call.
Exercise
Why does the API hand functions an AbortSignal instead of the AbortController itself?
Show answer
The signal can only be observed, so the function cannot cancel the operation on its own or on behalf of other consumers. The power to cancel stays with the caller who holds the controller.