← Back to JavaScript
38

Advanced fetch: cancel requests, retry safely and avoid races

Build resilient HTTP request flows with `AbortController`, timeouts, retry with backoff and protection against stale responses in dynamic UIs.

📘 Theory

Think in resilience, not only in success

The happy path is not enough when the interface depends on live data.

`fetch()` rejects on network failure and abort, but many HTTP errors still need manual validation through `response.ok`.

In live search or filter-heavy interfaces, cancellation matters just as much as success because late responses can overwrite newer UI state.

1

Abort

2

Retry

3

Validate

4

Isolate

Use `AbortController` and manual timeouts

If a request takes too long, stop it intentionally instead of freezing the interface.

Combining `AbortController` with `setTimeout()` gives the request a maximum allowed duration.

The key detail is cleanup: always clear the timer in `finally()` so it cannot fire later by accident.

  • Aborting a request is not always a fatal failure. Sometimes it is the correct UX decision.
  • Differentiate `AbortError` from other failures in the UI layer.
  • Keep timeout policy consistent across the app.

Retry with backoff for temporary failures

Do not retry everything blindly. Decide when a retry makes sense.

1

Aggressive retries can make a bad situation worse by adding even more load to a struggling system.

2

A short exponential backoff plus a clear retry limit is usually much safer than retrying forever or retrying immediately.

🧪 Learn by doing

Example Guided example: cancel a slow request with a timeout Build a helper that aborts when the request exceeds the allowed wait time.

🏁 Challenges

Challenge Challenge: implement `fetchWithTimeout()` Create a helper that aborts a request when it exceeds the configured maximum time.

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com