> The axios cancel token API is based on [cancelable promises proposal](https://github.com/tc39/proposal-cancelable-promises), which is currently at Stage 1.
> The axios cancel token API is based on the [cancelable promises proposal](https://github.com/tc39/proposal-cancelable-promises), which is currently at Stage 1.
You can create a cancel token by passing an executor function to the `CancelToken` constructor as shown below:
You can create a cancel token using the `CancelToken.source` factory as shown below:
```js
varCancel=axios.Cancel;
varCancelToken=axios.CancelToken;
varcancel;
varsource=CancelToken.source();
axios.get('/user/12345',{
cancelToken:newCancelToken(functionexecutor(c){
// An executor function receives a cancel function as a parameter
// You can use the cancel function to cancel the request later
cancel=c;
})
cancelToken:source.token
}).catch(function(thrown){
if(throwninstanceofCancel){
console.log('Request canceled',thrown.message);
...
...
@@ -491,20 +486,24 @@ axios.get('/user/12345', {
});
// cancel the request (the message parameter is optional)
cancel('Operation canceled by the user.');
source.cancel('Operation canceled by the user.');
```
You can also create a cancel token using the `CancelToken.source` factory:
You can also create a cancel token by passing an executor function to the `CancelToken` constructor:
```js
varCancelToken=axios.CancelToken;
varsource=CancelToken.source();
varcancel;
axios.get('/user/12345',{
cancelToken:source.token
cancelToken:newCancelToken(functionexecutor(c){
// An executor function receives a cancel function as a parameter