What do AbortController and AbortSignal do?
What AbortController is
AbortController is a controller that creates a signal (AbortSignal) and can "pull" it (cancel it).
In simpler terms:
AbortControlleris the "remote",AbortSignalis the "wire" plugged into the operation.
Example
const controller = new AbortController();
const { signal } = controller;
fetch('https://example.com/data', { signal })
.then(res => res.json())
.then(data => console.log('Success:', data))
.catch(err => {
if (err.name === 'AbortError') {
console.log('Request canceled!');
} else {
console.error('Error:', err);
}
});
// Cancel the request after 2 seconds
setTimeout(() => controller.abort(), 2000);What happens:
- A controller is created.
signalis pulled out of it.signalis passed intofetch.- After 2 seconds,
controller.abort()is called. fetchis interrupted and returns anAbortError.
How it works internally
AbortController
-
Has the methods:
javascriptcontroller.abort(reason?) // cancel the operation controller.signal // reference to the signal
AbortSignal
-
Holds the state:
javascriptsignal.aborted // true, if already canceled -
Can listen for an event:
javascriptsignal.addEventListener('abort', () => console.log('Canceled!'));
Example of working with several tasks
You can use a single controller for several operations at once:
const controller = new AbortController();
fetch('/users', { signal: controller.signal });
fetch('/posts', { signal: controller.signal });
// cancel both operations at once
controller.abort();Both requests will be interrupted.
You can specify a cancellation reason
controller.abort('Manual cancellation');
signal.addEventListener('abort', e => {
console.log(e.target.reason); // "Manual cancellation"
});(Supported in modern browsers and Node 18+)
Using it with Promise.race for a timeout
function fetchWithTimeout(url, ms) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), ms);
return fetch(url, { signal: controller.signal })
.finally(() => clearTimeout(timeout));
}
fetchWithTimeout('/api/data', 2000)
.then(r => r.json())
.then(console.log)
.catch(err => {
if (err.name === 'AbortError') console.log('Timeout!');
});A handy pattern: cancellation once the time runs out.
You can listen for cancellation manually (not only in fetch)
function wait(ms, signal) {
return new Promise((resolve, reject) => {
const timeout = setTimeout(resolve, ms);
signal.addEventListener('abort', () => {
clearTimeout(timeout);
reject(new Error('Canceled'));
});
});
}
const controller = new AbortController();
wait(5000, controller.signal).catch(console.error);
setTimeout(() => controller.abort(), 2000);Here wait() is a custom function,
but it can react to AbortSignal, just like fetch.
Support
Works in:
- all modern browsers (Chrome, Firefox, Safari, Edge)
- Node.js 16.5+ (in
fetch,stream,setTimeout)
A simple analogy
AbortControlleris the remote control.
AbortSignalis the wire connected to the task.When you press
abort(), the signal travels down the wire and tells the task: "Stop!"
SUMMARY
| Object | What it does | Main properties/methods |
|---|---|---|
AbortController | Manages cancellation | .abort(), .signal |
AbortSignal | Passed into an operation to listen for cancellation | .aborted, .addEventListener('abort') |
| Typical error | AbortError - thrown on cancellation | |
| Typical scenarios | Canceling fetch, timeouts, interrupting several tasks |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.