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 › framework › react › guides › query-cancellation
Query Cancellation | TanStack Query React Docs
When a query becomes out-of-date or inactive, this signal will become aborted. This means that all queries are cancellable, and you can respond to the cancellation inside your query function if desired.
Discussions

How do i cancel all queries which have already being run
How do i cancel all queries which have already being run More on github.com
🌐 github.com
1
1
How to cancel/abort a mutation query?
The thing is: queries have no side-effect on the server, so you can run them as often as you want and also cancel them if you 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 ... More on github.com
🌐 github.com
23
11
January 1, 2021
Disable all queries in the application
I am using react-query for all API calls happening in an application. However, there are some cases that these API calls might fail, for example, the authentication of the user is no longer valid. Is it possible to catch that error and disable all queries that are in the application. More on github.com
🌐 github.com
1
1
Help with React Query onMutate function.
Where is the rest of your code? You’re mutating the store using the same query key for an index of & individual records which could be the issue More on reddit.com
🌐 r/reactjs
3
1
September 20, 2021
🌐
TanStack
tanstack.com › query › v4 › docs › react › guides › query-cancellation
Query Cancellation | TanStack Query Docs
TanStack Query provides each query function with an AbortSignal instance, if it's available in your runtime environment. When a query becomes out-of-date or inactive, this signal will become aborted. This means that all queries are cancellable, and you can respond to the cancellation inside ...
🌐
GitHub
github.com › TanStack › query › discussions › 5266
How do i cancel all queries which have already being run · TanStack/query · Discussion #5266
Hi, 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...
Author   TanStack
🌐
Carl Rippon
carlrippon.com › cancelling-requests-with-react-query
Cancelling Requests with React Query
There is a cancelQueries function that can be called on the React Query client to cancel requests.
🌐
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
When a query becomes out-of-date or inactive, this signal will become aborted. This means that all queries are cancellable, and you can respond to the cancellation inside your query function if desired.
Author   TanStack
Find elsewhere
🌐
TanStack
tanstack.com › query › v5 › docs › framework › angular › guides › query-cancellation
Query Cancellation | TanStack Query Angular Docs
When a query becomes out-of-date or inactive, this signal will become aborted. This means that all queries are cancellable, and you can respond to the cancellation inside your query function if desired.
🌐
TanStack
tanstack.com › query › v5 › docs › framework › react › guides › filters
Filters | TanStack Query React Docs
A query filter is an object with certain conditions to match a query with: ... // Cancel all queries await queryClient.cancelQueries() // Remove all inactive queries that begin with `posts` in the key queryClient.removeQueries({ queryKey: ['posts'], ...
🌐
TanStack
tanstack.com › query › latest › docs › framework › react › guides › query-invalidation
Query Invalidation | TanStack Query React Docs
If you find yourself wanting even more granularity, you can pass a predicate function to the invalidateQueries method. This function will receive each Query instance from the query cache and allow you to return true or false for whether you want to invalidate that query:
🌐
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.
🌐
TanStack
tanstack.com › query › v4 › docs › framework › react › guides › disabling-queries
Disabling/Pausing Queries | TanStack Query React Docs
function Todos() { const [filter, setFilter] = React.useState('') const { data } = useQuery({ queryKey: ['todos', filter], queryFn: () => fetchTodos(filter), // ⬇️ disabled as long as the filter is empty enabled: !!filter }) return ( <div> // 🚀 applying the filter will enable and execute the query <FiltersForm onApply={setFilter} /> {data && <TodosTable data={data}} /> </div> ) } Lazy queries will be in status: 'loading' right from the start because loading means that there is no data yet.
🌐
TanStack
tanstack.com › query › v3 › docs › framework › react › reference › QueryClient
QueryClient | TanStack Query React Docs
By default, it will not throw an ... to true · The cancelQueries method can be used to cancel outgoing queries based on their query keys or any other functionally accessible property/state of the query....
🌐
DEV Community
dev.to › serifcolakel › canceling-requests-in-reactreact-native-a-comprehensive-guide-2ami
Canceling Requests in React/React Native: A Comprehensive Guide - DEV Community
April 6, 2025 - Axios: Using CancelToken to cancel requests. Fetch: Leveraging the AbortController API. React Query: Built-in cancellation support.
🌐
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
I read the Query Cancellation post, but my queries not cancelled. Currently I have something like that · export const axiosInstance = ({method, url, ...rest}) => { const source = axios.CancelToken.source(); const promise = axios({ method, url, cancelToken: source.token, ...rest, }); // TODO: This not cancel the request promise.cancel = () => { source.cancel("Query was cancelled by React Query"); }; return promise; }; Beta Was this translation helpful?
Author   TanStack
🌐
TanStack
tanstack.com › query › latest › docs › reference › QueryClient
QueryClient | TanStack Query Docs
Queries that are "static" because ... be refetched. The cancelQueries method can be used to cancel outgoing queries based on their query keys or any other functionally accessible property/state of the query....