AbortController AbortController Request Cancellation

Last updated: 2026-11-02

AbortController AbortController Request Cancellation

AbortController is a native browser API: pass its signal to fetch and call abort() to cancel the request halfway. Unmounting a component, changing a parameter and debouncing a search all benefit from it - cancelling outdated requests saves bandwidth and, more importantly, avoids the race where a stale response overwrites fresh data that has already been rendered. The same controller can also abort many requests at once, so one cleanup routine covers every in-flight call a component started.

GroupData & Engineering
KindAPI
SinceNative browser API (DOM Living Standard)

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

const ctrl = new AbortController()

fetch('/api/search?q=' + kw, { signal: ctrl.signal })
  .then(r => r.json())
  .catch(err => {
    if (err.name === 'AbortError') return   // cancelled on purpose, not an error
    showError(err)
  })

ctrl.abort()   // triggers it: fetch rejects with an AbortError

⚙️ Parameters

signalThe AbortSignal instance, passed to fetch as options.signal; the same signal can drive several requests and cancel all of them at once.

Returns:The constructor returns { signal, abort() }: the signal is what fetch listens to, abort() triggers the cancellation and the request rejects with an AbortError.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

Clicking "Send request" enters the loading state; click "Cancel request" before the response arrives and the state becomes aborted instead of error - a cancelled request never updates the UI.

💡 Key Points

⚠️ Warning A cancellation is not an error: the catch block must recognize err.name === 'AbortError' on its own, otherwise a deliberate cancel pops up an error message for the user.
💡 Tip Aborting the previous request in the cleanup of an Effect is the standard cure for the race that scrambles the data when you switch tabs quickly.
ℹ️ Info The same signal can also be handed to event listeners (the signal option of addEventListener), so one abort cancels all of them.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Can a cancelled request be resumed?
A No, the connection is already closed. If you need pause and resume semantics you have to implement a queue yourself, or simply fire the request again.
Q Is an AbortError the same as a timeout?
A No. A timeout can be implemented with setTimeout(() => ctrl.abort(), 5000), and in newer browsers AbortSignal.timeout(5000) does it in one line.
Q Why cancel the request when the component unmounts?
A After unmount the component no longer needs the data; letting the request run wastes bandwidth and the response may even write stale data into a later render.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏