AbortController — Cancellable Fetch
debt(d7/e3/b3/t7)
Closest to 'only careful code review or runtime testing' (d7). The detection_hints note ESLint as the available tool but mark automated detection as 'no', meaning the pattern (fetch inside rapid-input handlers without AbortController) requires manual code review or observing race conditions at runtime. ESLint has no built-in rule for this pattern, so it won't be caught automatically.
Closest to 'simple parameterised fix' (e3). The quick_fix states: store the controller, call .abort() before each new request, and catch AbortError separately. This is a small, localised refactor within the component containing the fetch call — not a one-liner, but contained to one file/component.
Closest to 'localised tax' (b3). The applies_to scope is web contexts only, and the fix is component-scoped. Once AbortController is wired into a fetch handler, it imposes minimal ongoing burden; only the component housing the fetch pays the cost. It does not spread architectural weight across the codebase.
Closest to 'serious trap' (t7). The misconception field explicitly states that aborting a fetch only closes the browser connection and does NOT stop PHP execution server-side — PHP continues unless it checks connection_aborted(). This directly contradicts the intuitive expectation that 'cancel' means the work stops everywhere, making it a serious cross-layer trap that contradicts how cancellation works in other contexts.
Also Known As
TL;DR
Explanation
new AbortController() creates a controller with a .signal property. Pass signal to fetch() options. Call controller.abort() to cancel. fetch() rejects with AbortError. Pattern: cancel previous request before starting a new one (debounced search). AbortSignal.timeout(ms) creates a one-shot timeout signal without needing a manual controller. Prevents race conditions where an older slow response arrives after a newer fast one.
Common Misconception
Why It Matters
Common Mistakes
- Not catching AbortError separately from real errors
- Creating a new controller without aborting the previous one first
- Expecting abort to stop PHP execution server-side
Code Examples
// Race condition — old slow response overwrites new fast one:
async function search(q) {
const data = await fetch(`/api/search?q=${q}`).then(r => r.json());
renderResults(data); // may render stale results
}
let controller;
async function search(q) {
controller?.abort(); // Cancel previous request
controller = new AbortController();
try {
const res = await fetch(`/api/search?q=${q}`, { signal: controller.signal });
const data = await res.json();
renderResults(data);
} catch (e) {
if (e.name !== 'AbortError') throw e; // ignore cancellation
}
}
// Timeout without manual controller:
fetch('/api/data', { signal: AbortSignal.timeout(5000) });