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.
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.
You need to throw out an error based on the response in your getData(). React-Query will catch that error and trigger the onError event.