Skip to content

Commit e84f0af

Browse files
author
Amr Abdelkader
committed
docs: update docs/usage.md
1 parent 74daad4 commit e84f0af

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

docs/usage.md

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,37 @@ methods: {
3838

3939
### Cancel token
4040

41-
You can cancel a request using a *cancel token*.
41+
You can cancel a request using a _cancel token_.
4242

4343
> The axios cancel token API is based on the withdrawn [cancelable promises proposal](https://github.com/tc39/proposal-cancelable-promises).
4444
4545
You can create a cancel token using the `CancelToken.source` factory as shown below:
4646

4747
```js
48-
const CancelToken = this.$axios.CancelToken;
49-
const source = CancelToken.source();
48+
const { CancelToken } = this.$axios;
49+
const source = this.$axios.CancelToken.source();
5050

51-
this.$axios.$get('/user/12345', {
52-
cancelToken: source.token
53-
}).catch(function (thrown) {
54-
if (this.$axios.isCancel(thrown)) {
55-
console.log('Request canceled', thrown.message);
56-
} else {
57-
// handle error
58-
}
59-
});
60-
61-
this.$axios.$post('/user/12345', {
62-
name: 'new name'
63-
}, {
64-
cancelToken: source.token
65-
})
51+
this.$axios
52+
.$get('/user/12345', {
53+
cancelToken: source.token,
54+
})
55+
.catch(error => {
56+
if (this.$axios.isCancel(error)) {
57+
console.log('Request canceled', error);
58+
} else {
59+
// handle error
60+
}
61+
});
62+
63+
this.$axios.$post(
64+
'/user/12345',
65+
{
66+
name: 'new name',
67+
},
68+
{
69+
cancelToken: source.token,
70+
},
71+
);
6672

6773
// cancel the request (the message parameter is optional)
6874
source.cancel('Operation canceled by the user.');
@@ -71,14 +77,14 @@ source.cancel('Operation canceled by the user.');
7177
You can also create a cancel token by passing an executor function to the `CancelToken` constructor:
7278

7379
```js
74-
const CancelToken = this.$axios.CancelToken;
80+
const { CancelToken } = this.$axios;
7581
let cancel;
7682

7783
this.$axios.$get('/user/12345', {
78-
cancelToken: new CancelToken(function executor(c) {
84+
cancelToken: new CancelToken(c => {
7985
// An executor function receives a cancel function as a parameter
8086
cancel = c;
81-
})
87+
}),
8288
});
8389

8490
// cancel the request

0 commit comments

Comments
 (0)