AbortController landed in browsers in 2017 and is best known as the mechanism for cancelling fetch() requests. A controller exposes an AbortSignal that can be passed into an abortable API; calling abort() on the controller tears down the work it was attached to — including several requests at once.
Cancelling requests and timers
The canonical use is a single call: create a controller, hand its signal to fetch(), and abort when the response is no longer wanted.
const controller = new AbortController();
const res = fetch('/', { signal: controller.signal });
controller.abort();
console.log(res); // => Promise(rejected): "DOMException: The user aborted a request"
The same signal drops neatly into setTimeout, so a fetch can be given a deadline — say, ten seconds — without extra bookkeeping.
function timeout(duration, signal) {
return new Promise((resolve, reject) => {
const handle = setTimeout(resolve, duration);
signal?.addEventListener('abort', e => {
clearTimeout(handle);
reject(new Error('aborted'));
});
});
}
// Usage
const controller = new AbortController();
const promise = timeout(10000, controller.signal);
controller.abort();
console.log(promise); // => Promise(rejected): "Error: aborted"
Retiring listeners instead of removing them
The newer development is that addEventListener itself accepts an Abort Signal, starting with Chrome 88. That makes the controller a drop-in alternative to removeEventListener.
const controller = new AbortController();
eventTarget.addEventListener('event-type', handler, { signal: controller.signal });
controller.abort();
What raises this above a one-for-one replacement is the controller's ability to abort multiple cancelable requests in a single call, so a group of listeners can be removed together instead of one at a time. Drag and drop is a natural fit.
Hand-written without AbortController, such a script pairs two removeEventListener calls with the two events it registered in order to clean up after itself.
// With removeEventListener
el.addEventListener('mousedown', e => {
if (e.buttons !== 1) return;
const onMousemove = e => {
if (e.buttons !== 1) return;
/* work */
}
const onMouseup = e => {
if (e.buttons & 1) return;
window.removeEventListener('mousemove', onMousemove);
window.removeEventListener('mouseup', onMouseup);
}
window.addEventListener('mousemove', onMousemove);
window.addEventListener('mouseup', onMouseup); // Can’t use `once: true` here because we want to remove the event only when primary button is up
});
Using the new signature, addEventListener takes the signal property as its second argument. One abort() call then stops every listener bound to that signal once they are no longer needed.
// With AbortController
el.addEventListener('mousedown', e => {
if (e.buttons !== 1) return;
const controller = new AbortController();
window.addEventListener('mousemove', e => {
if (e.buttons !== 1) return;
/* work */
}, { signal: controller.signal });
window.addEventListener('mouseup', e => {
if (e.buttons & 1) return;
controller.abort();
}, { signal: controller.signal });
});
Browser support
Chrome 88 is currently the only browser where addEventListener officially accepts an AbortSignal. Firefox and Safari support AbortController itself, but wiring its signal into addEventListener does not work there, and there is no indication of plans to implement it. A polyfill is available for cases where the behavior is needed across browsers.



