Request cancellation

Request cancellation uses an AbortController: its signal, passed to fetch, interrupts the request when you call abort(). fetch then rejects with an AbortError, which you must distinguish from a real error. It avoids stale responses and wasted work when the user changes screen or input.

Example

const c = new AbortController();
fetch(url, { signal: c.signal });
c.abort();

Related concepts

The whole glossary