Stas Kyaune’s Post

Most developers use fetch without ever canceling it. That means: stale responses overwriting fresh ones, memory leaks in unmounted components, and race conditions that are hell to debug. AbortController fixes all of this — and it's been native since ES2017. Two main use cases: 1. Cancel on component unmount — so old requests don't update state after a user navigated away. 2. Cancel on new request — so if a user types fast in a search field, only the last request wins. No library needed. Do you use AbortController in your projects? 👇 #JavaScript #WebDevelopment #Frontend #JS #SoftwareEngineering

  • text

Even better would be to write a common useFetch hook and later use it with various modifiers for each special case. Plus, you can add custom TTL control and a retry mechanism.

TanStack Query can handle this for you, and it comes with a lot more features and benefits.

In my case it was axios canceltoken, rather than AbortController directly.

I didn't know about that, thanks for sharing!

See more comments

To view or add a comment, sign in

Explore content categories