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.
How to cancel/abort a mutation query?
How do i cancel all queries which have already being run
In a filter search UI how important is it cancel requests when user makes quick changes?
How to cancel all prior requests and only display results from the latest API call.
Videos
Let's consider a scenario of a website like Amazon in which a user can choose filters. After every filter that is selected or unselected an API call has to be made. If a user selects four filters in a row, four API calls have to be made. We do not want to display the results we received from the first three filters and only want to display those from the last request. How do we implement such functionality?
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.