← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · ✎ updated 7d
← Back to glossary

AbortController — Cancellable Fetch

JavaScript ES2017 Intermediate
debt(d7/e3/b3/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

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.

e3 Effort Remediation debt — work required to fix once spotted

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.

b3 Burden Structural debt — long-term weight of choosing wrong

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.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

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.

About DEBT scoring →

Also Known As

AbortController fetch cancellation AbortSignal request cancellation

TL;DR

AbortController cancels in-flight fetch requests — essential for search-as-you-type, tab switching, and preventing race conditions in PHP API calls.

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

✗ Aborting a fetch request also cancels PHP's processing on the server — aborting only closes the browser connection; PHP will continue executing unless it checks connection_aborted()

Why It Matters

Search-as-you-type without AbortController sends one request per keystroke — earlier responses arriving out of order corrupt the UI; abort-and-retry ensures only the latest response is shown.

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

✗ Vulnerable
// 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
}
✓ Fixed
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) });

Added 17 Mar 2026
Edited 22 Mar 2026
Views 222
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping S 1 ping S 1 ping M 1 ping T 0 pings W 0 pings T 3 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S
No pings yet today
No pings yesterday
ChatGPT 20 Perplexity 13 Amazonbot 12 PetalBot 12 SEMrush 11 Scrapy 11 Ahrefs 10 Google 7 Bing 6 Unknown AI 4 Twitter/X 3 Applebot 2 Sogou 2 Majestic 1 Meta AI 1 Brave Search 1 Baidu 1
crawler 115 crawler_json 1 pre-tracking 1
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
JavaScript javascript The programming language of the browser — it reads and modifies the page (the DOM), reacts to user events, and fetches data without reloading.

JavaScript is the only language browsers execute, so every interactive behaviour on the web goes through it. Its two defining traits — single-threaded event loop and loose typing (== coercion) — explain the majority of both its bugs and its design patterns.

💡 Default to const, use === always, and reach for let only when a value genuinely reassigns.

Ask Codex about JavaScript →
⚡ DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Store the controller, call .abort() before each new request, and catch AbortError separately
📦 Applies To
javascript ES2017 web
🔗 Prerequisites
🔍 Detection Hints
fetch inside search input handler or rapid-click handler without AbortController; race conditions from multiple concurrent fetches
Auto-detectable: ✗ No eslint
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Low Context: Function Tests: Update


✓ schema.org compliant