You cannot attach .cancel to a promise in an async functions because async functions will always return a new Promise without the cancel function you attached. If you want to attach a cancel functions, it's best to use promise chaining instead of async functions.

Even better, react-query supports cancellation now by providing an AbortSignal out of the box, which you only need to attach to your request.

with axios 0.22+ and react-query v3.30+, you can do:

 const query = useQuery('key', ({ signal }) =>
   axios.get('/xxxx', {
     signal,
   })
 )

with that, react-query will cancel the network request of your query if the query becomes unused or if you call queryClient.cancelQueries('key'), e.g. when the user clicks a button.

Answer from TkDodo on Stack Overflow
🌐
TanStack
tanstack.com › query › v4 › docs › react › guides › query-cancellation
Query Cancellation | TanStack Query Docs
To do this, you just need to call queryClient.cancelQueries({ queryKey }), which will cancel the query and revert it back to its previous state. If you have consumed the signal passed to the query function, TanStack Query will additionally also cancel the Promise.
Discussions

How to cancel/abort a mutation query?
The thing is: queries have no ... are no longer interested in the result. Per default, react-query will only ignore the result, but you can also attach a cancel method to the Promise to really abort the network request.... More on github.com
🌐 github.com
23
11
January 1, 2021
How do i cancel all queries which have already being run
this invalidates and tries to download a fresh for all queries. While this downloads and completes i show a loading spinner and a cancel button to cancel this . Cancel button code is as follows ... to cancel all request made by the invalidateQueries but this does not work properly. More on github.com
🌐 github.com
1
1
In a filter search UI how important is it cancel requests when user makes quick changes?
🌐 r/reactjs
27
9
September 19, 2024
How to cancel all prior requests and only display results from the latest API call.
You want to look up the Abort Controller API . It will let you cancel fetch requests that are in flight. What you can do is keep a reference to the controller while you are waiting for a response. If a new request is made you use the controller to cancel the old one. More on reddit.com
🌐 r/reactjs
71
55
June 28, 2021
🌐
TanStack
tanstack.com › query › v3 › docs › framework › react › guides › query-cancellation
Query Cancellation | TanStack Query React Docs
To do this, you just need to call queryClient.cancelQueries(key), which will cancel the query and revert it back to its previous state. If promise.cancel is available, or you have consumed the signal passed to the query function, React Query ...
🌐
Carl Rippon
carlrippon.com › cancelling-requests-with-react-query
Cancelling Requests with React Query
We have added the PromiseWithCancel interface and used this on the returned promised, otherwise a type error would occur. There is a cancelQueries function that can be called on the React Query client to cancel requests.
🌐
DEV Community
dev.to › rahuls24 › how-to-handle-rtk-query-request-cancellations-in-react-292h
How to Handle RTK-Query Request Cancellations in React - DEV Community
July 23, 2025 - After digging around, I realized the fix was simpler than expected: handle cancellations properly using useLazyQuery. When you're making rapid API calls — like in autocomplete, filters, or live search — older requests may return after newer ones. RTK-Query will still fulfill those old requests unless you actively cancel them.
🌐
Medium
medium.com › @serifcolakel › canceling-requests-in-react-react-native-a-comprehensive-guide-b1be32dcf823
Canceling Requests in React/React Native: A Comprehensive Guide | by Serif Colakel | Medium
April 6, 2025 - Axios: Using CancelToken to cancel requests. Fetch: Leveraging the AbortController API. React Query: Built-in cancellation support.
Find elsewhere
🌐
GitHub
github.com › TanStack › query › discussions › 5266
How do i cancel all queries which have already being run · TanStack/query · Discussion #5266
I have this functionality in a react native app which on click of the button calls the · queryClient.invalidateQueries() this invalidates and tries to download a fresh for all queries. While this downloads and completes i show a loading spinner and a cancel button to cancel this . Cancel button code is as follows · await queryClient.cancelQueries() to cancel all request made by the invalidateQueries but this does not work properly.
Author   TanStack
🌐
YouTube
youtube.com › watch
Cancelling a request in React Query in 6 minutes
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Reddit
reddit.com › r/reactjs › in a filter search ui how important is it cancel requests when user makes quick changes?
In a filter search UI how important is it cancel requests when user makes quick changes? : r/reactjs
September 19, 2024 - Join the Reactiflux Discord (reactiflux.com) for additional React discussion and help. ... Sorry, this post was deleted by the person who originally posted it. Share ... Unless you actually get the server to cancel processing, it doesn't matter if you just forget you made the request and use the new one, or cancelling an inflight request.
🌐
Run, Code & Learn
blog.delpuppo.net › react-query-abort-request
React Query - Abort Request - Run, Code & Learn
May 17, 2023 - Hey Folks, Today it's time to learn how you can abort an ongoing request with ReactQuery. Before moving to the example, I want to introduce the AbortSignal. AbortSignal is a javascript interface used to abort ongoing methods. Typically, you ...
🌐
GitHub
github.com › TanStack › query › discussions › 1283
Query Cancellation · TanStack/query · Discussion #1283
As far as I'm aware, the cancel method will only be called if your query becomes inactive while the request is still in-flight. A query becomes inactive when it has no observers, which basically means: all components that use it via useQuery have unmounted.
Author   TanStack
🌐
GitHub
github.com › TanStack › query › blob › main › docs › framework › react › guides › query-cancellation.md
query/docs/framework/react/guides/query-cancellation.md at main · TanStack/query
This is helpful if you've started receiving a query, but then unmount the component before it finishes. If you mount the component again and the query has not been garbage collected yet, data will be available. However, if you consume the AbortSignal, the Promise will be cancelled (e.g.
Author   TanStack
🌐
Answer Overflow
answeroverflow.com › m › 1397912201398063194
Query Cancellation - TanStack
July 24, 2025 - The old request stays (pending) in the Network tab and never aborts. I tried: Passing signal (as above) Adding ... queryClient.cancelQueries({ queryKey: ['media'], exact: false }) in a useEffect and before state changes → still no abort / no log Question: What’s the correct way to cancel an in-flight query when switching to a different key?
🌐
TanStack
tanstack.com › query › v5 › docs › framework › angular › guides › query-cancellation
Query Cancellation | TanStack Query Angular Docs
This is helpful if you've started receiving a query, but then unmount the component before it finishes. If you mount the component again and the query has not been garbage collected yet, data will be available. However, if you consume the AbortSignal, the Promise will be cancelled (e.g.
🌐
Reddit
reddit.com › r/reactjs › do i need to manage fetch cancellations? or does react handle this?
r/reactjs on Reddit: Do I need to manage fetch cancellations? Or does React handle this?
September 22, 2021 -

Let's say I have a request which takes enough time for a user to navigate away before completion (maybe they change there mind, etc), I want this request to be cancelled to prevent unwanted behaviour. Does React automatically handle this? Or do I need to somehow cancel the fetch request?

In my case I have a modal with user authentication form, and am trying to figure out how to cancel the request if the user closes the modal before the request finishes, although this is not an issue with this modal as the response time is <1s I could see this occurring at some point in the future with other request types. I don't want the user to close the modal and the request to still complete as that does not make sense.

Any ideas would be great, I feel I am probably overthinking this.