Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add CancelToken and isCancel to axios instance #292

Merged
merged 6 commits into from Oct 23, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 21 additions & 28 deletions docs/usage.md
Expand Up @@ -45,50 +45,43 @@ You can cancel a request using a _cancel token_.
You can create a cancel token using the `CancelToken.source` factory as shown below:

```js
const { CancelToken } = this.$axios;
const source = this.$axios.CancelToken.source();
const source = this.$axios.CancelToken.source()

this.$axios
.$get('/user/12345', {
cancelToken: source.token,
this.$axios.$get('/user/12345', {
cancelToken: source.token
}).catch(error => {
if (this.$axios.isCancel(error)) {
console.log('Request canceled', error)
} else {
// handle error
}
})

this.$axios.$post('/user/12345', {
name: 'new name'
}, {
cancelToken: source.token
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation

})
.catch(error => {
if (this.$axios.isCancel(error)) {
console.log('Request canceled', error);
} else {
// handle error
}
});

this.$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.');
source.cancel('Operation canceled by the user.')
```

You can also create a cancel token by passing an executor function to the `CancelToken` constructor:

```js
const { CancelToken } = this.$axios;
let cancel;
const { CancelToken } = this.$axios
let cancel

this.$axios.$get('/user/12345', {
cancelToken: new CancelToken(c => {
// An executor function receives a cancel function as a parameter
cancel = c;
cancel = c
}),
});
})

// cancel the request
cancel();
cancel()
```

> Note: you can cancel several requests with the same cancel token.