Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

axios cancelled requests can't be restarted

I'm cancelling axios requests when the component unmounts as following

const CancelToken = axios.CancelToken
const source = CancelToken.source()

componentWillUnmount () {
 source.cancel('message')
}

When the component is unmounted, the network requests happening in the background are flagged as 'stalled'. The problem is when the user goes to that component again, network requests supposed to happen in componentDidMount do no start.

like image 385
Shakil Ahmed Avatar asked Nov 26 '18 05:11

Shakil Ahmed


People also ask

Is CancelToken deprecated?

CancelToken from axios is deprecated, so I suggest updating the documentation to use AbortController instead.

How do I cancel Axios POST request?

You can also cancel a request using a CancelToken. The axios cancel token API is based on the withdrawn cancelable promises proposal. You can create a cancel token using the CancelToken.

What is a cancel token?

A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects. You create a cancellation token by instantiating a CancellationTokenSource object, which manages cancellation tokens retrieved from its CancellationTokenSource.

How do I cancel a request in Axios?

The syntax is straightforward: you pass a cancelToken option to your Axios request, and calling cancel () makes your request error out. The options parameter to Axios functions supports a cancelToken option.

How do I create a cancel token in Axios?

The syntax is straightforward: you pass a cancelToken option to your Axios request, and calling cancel () makes your request error out. The options parameter to Axios functions supports a cancelToken option. The Axios global has a CancelToken object that can create a cancel token for you as shown below.

Why am I getting Axios request timeout errors?

The default timeout for Axios' requests may be longer than the timeout for the parent process, which will swallow this error. Manually set your Axios request timeout to be short and you'll see the error, e.g. Sorry, something went wrong. could somebody explain what is the status of cancelled ?

How do I abort a request in Axios?

This can be achieved by using AbortController, which is an inbuilt browser interface. *Note: this works with fetch, axios has its own implementation Signal represents a signal object that allows you to communicate with a DOM request (such as a Fetch) and abort it if required via an AbortController object.


Video Answer


2 Answers

axios#cancellation describes two ways to use the cancelToken. You used the first way, with source.token/source.cancel. I started out that way, and had a similar problem as yours: Once a request to a particular URL was canceled, I could never get a successful response from that URL again. I switched to the second method using an executor function and the problem went away. I guess was sharing the same cancelToken for multiple requests, which is what they say you can do with the executor function method. Anyway, maybe that would work for you too.

like image 139
Lannie Avatar answered Oct 12 '22 04:10

Lannie


After canceling set a new token.

let uploadCancelTokenSource = axios.CancelToken.source();

upload() {
    const config = {
        cancelToken: uploadCancelTokenSource.token,
    };  
}

cancel() {
    uploadCancelTokenSource.cancel('Operation canceled by the user');
    uploadCancelTokenSource = axios.CancelToken.source();
}
like image 32
Pax Exterminatus Avatar answered Oct 12 '22 03:10

Pax Exterminatus