0👍
First off, just know CancelToken
according to docs:
is deprecated since v0.22.0 and shouldn’t be used in new projects
To use CancelToken
, there’s just 3 steps:
- create a
CancelToken
- assign that token to a request
- invoke the method on
CancelToken
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
axios.post('/user/12345', {
name: 'new name'
}, {
cancelToken: source.token
})
// cancel the request (the message parameter is optional)
source.cancel('Operation canceled by the user.');
or
const CancelToken = axios.CancelToken;
let cancel;
axios.get('/user/12345', {
cancelToken: new CancelToken(function executor(c) {
// An executor function receives a cancel function as a parameter
cancel = c;
})
});
// cancel the request
cancel();
Source:stackexchange.com