Skip to the solution
MediumFrontend6 min read

How do you abort a Fetch API request using AbortController?

58 views

The problem

Question presented to candidate: "A user navigates away from a search page before the results fetch() finishes. How would you cancel that in-flight request, and what exactly does the fetch's Promise reject with?"

What a strong answer should cover:

  • πŸ“Œ Interview term: AbortController β€” a real, built-in object exposing a single .abort(reason?) method and a .signal property (an AbortSignal) that can be passed to fetch()'s own { signal } option to make that specific request cancellable.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: calling controller.abort() with no argument genuinely produces a real, default DOMException named "AbortError" as signal.reason, and the in-flight fetch()'s Promise genuinely rejects with exactly that DOMException.
  • πŸ“Œ Interview term: the real custom-reason distinction β€” verified directly: calling controller.abort("some custom reason") makes signal.reason genuinely the EXACT custom value passed, and the fetch() Promise genuinely rejects with THAT value directly β€” not wrapped in a DOMException β€” a real, sharp, easy-to-miss distinction from the default no-argument case.
  • πŸ“Œ Interview term: pre-aborting β€” a precise answer names that a signal aborted BEFORE fetch() is even called genuinely causes the fetch to reject immediately β€” verified directly β€” the network request never actually starts at all.
  • A precise answer names the real, practical cleanup pattern: calling .abort() in a useEffect cleanup function (or equivalent lifecycle hook) when a component unmounts or a dependency changes, preventing a real, classic "setState after unmount" warning/bug from a request that finishes after it is no longer needed.

Clarifying questions expected:

  • None β€” this is a definitional/practical question; directly demonstrating the real, verified rejection value in both the default and custom-reason cases is the strong signal.

Code / implementation expected: Yes β€” a real AbortController cancelling a real in-flight fetch(), verified with both a default abort and a custom-reason abort, plus the pre-abort case.

  • abortcontroller
  • fetch
  • promises

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: AbortController.abort() with no argument rejects with a real AbortError DOMException; a custom reason rejects with that exact value β€” verified against a real network endpoint

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 118 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

Explain the event loop, call stack, and task queues.

Continue