AbortController

The standard JavaScript object for cancelling asynchronous work: its signal is handed to the work, and calling abort() cancels it.

In more detail

One controller can cancel a fetch, remove event listeners and stop your own loops at once, because they all watch the same signal. abort(reason) records why, and the reason becomes the error the cancelled work rejects with.

Examples

const ctrl = new AbortController(); fetch(url, { signal: ctrl.signal }); ctrl.abort();
In Node.js, stream.pipeline(), fs.watch(), timers/promises and events.once() also accept an AbortSignal.
Creating a new controller inside an Effect and aborting it in cleanup cancels outdated requests when dependencies change or the component unmounts.
const controller = new AbortController(); fetch(url, { signal: controller.signal }); return () => controller.abort();

Related terms

Taught in

References