r/reactjs • u/kevin074 • Jul 12 '26
Needs Help is abort controller commonly used?
as title suggests, I have not seen this at work before and gemini teaches me about this, just curious whether this is actually commonly used/best practice or just another AI slop. Thanks!
13
Upvotes
37
u/RobertKerans Jul 12 '26
It's just a standard piece of the web API. If I want to be able to abort async operations (so fetch primarily) I'm going to use the thing that allows me to abort them, it's a basic core piece of functionality that's been available for years.
12
u/Renan_Cleyson Jul 12 '26
It's best practice, many things are meant to support an abort controller through the JS Web APIs to make especially async things easier btw
12
u/thesonglessbird Jul 12 '26
I failed a coding interview a few years ago because I didn’t use it.
57
u/PineappleFabulous971 Jul 12 '26
I've used it mainly on long uploading tasks in those X to cancel upload buttons, its not a slop technique
3
u/LiveRhubarb43 Jul 12 '26
It's not slop, but it's also not related to react
6
u/Civil_Cheesecake2492 Jul 12 '26
Pretty much if you're doing a manual fetch in a useEffect in react you should pass an AbortController signal to fetch and in the cleanup return function for the useEffect call controller.abort(). Also in your catch block for the fetch check for the error.name 'AbortError' and ignore it (generally just return).
2
u/hyrumwhite Jul 12 '26
I use it to cancel event listeners most commonly
1
u/_suren Jul 12 '26
I mainly use it when the user can replace an in-flight request: search-as-you-type, route change, upload cancel. If React Query or similar owns the fetch, let the library pass the signal. Don’t add one to every effect just because AI suggested it.
1
u/TheRealSeeThruHead Jul 12 '26 edited Oct 14 '26
Commodo culpa proident voluptate nostrud cupidatat in culpa aliqua irure. Deserunt sit anim duis culpa sed adipiscing commodo. Velit ullamco cillum lorem commodo incididunt duis anim esse voluptate sunt fugiat non ullamco fugiat sed ullamco consectetur.
1
u/BlazingThunder30 Jul 12 '26
I'm using Tanstack Query with fetch. I think that uses it under the hood to cancel ongoing request when page navigations occur or when parameters to a request change. Why wouldn't you use it?
1
u/lightfarming Jul 12 '26
it is standard. aborts calls on page changes, or second similar requests. prevents race conditions if two of the same endpoint calls go out, and the wrong one (first call) responds last.
in tanstack query, it passes you a signal argument that you can use directly, instead of handling the abort controller yourself.
1
u/Ok-Tune-1346 Jul 12 '26
Yup, very useful, no reason to not cancel a request if you know it is no longer needed. more useful for slower internet speeds... if you're on cable it makes little difference.
Not just useful on FE either.
AbortSignal.timeout is useful too - I would say that is more rare (it is a newer feature)
1
u/Plus-Weakness-2624 Jul 12 '26
I use that with my GF all the time during "pair" programming!
1
u/OkBoomer421 Jul 12 '26
Yeah, pretty standard for cancelling fetch requests when a component unmounts. Also handy for race condition cleanup — fire multiple requests, cancel the losers.
1
u/Captain_Cowkill Jul 13 '26
It is alas not as commonly used as it should be.
1
u/confused_crocodile Jul 28 '26
Very useful and best practice. I use it in backend JS/TS. JS promises are eager so you need a way to stop them. This is generally what AbortControllers are for. It’s verbose, but I actually think the explicitness is very helpful for concurrent code
1
u/Lumethys Jul 12 '26
It's a piece of web standard. It can prevent a common class of bug.
Suppose you have a table with a search bar. You search "Johnathan". The UI fire 2 requests. One for "Johna" and one for "Johnathan".
Even though "Johna" fire first, but due to various things, the result might arrives after "Johnathan", so the search bar contains the text "Johnathan", but the result in the table is for "Johna"
It is not the only thing that can avoid this type of bug, but it is one of the standard way to prevent it
0
u/eindbaas Jul 12 '26
I am so confused, how do you even end up posting on reddit asking whether something like that is AI slop? You are aware that you can simply look it up and read what it is and does?
0
u/[deleted] Jul 12 '26
[deleted] — view deleted comment