@@ -38,31 +38,37 @@ methods: {
38
38
39
39
### Cancel token
40
40
41
- You can cancel a request using a * cancel token * .
41
+ You can cancel a request using a _ cancel token _ .
42
42
43
43
> The axios cancel token API is based on the withdrawn [ cancelable promises proposal] ( https://github.com/tc39/proposal-cancelable-promises ) .
44
44
45
45
You can create a cancel token using the ` CancelToken.source ` factory as shown below:
46
46
47
47
``` js
48
- const CancelToken = this .$axios . CancelToken ;
49
- const source = CancelToken .source ();
48
+ const { CancelToken } = this .$axios ;
49
+ const source = this . $axios . CancelToken .source ();
50
50
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
+ );
66
72
67
73
// cancel the request (the message parameter is optional)
68
74
source .cancel (' Operation canceled by the user.' );
@@ -71,14 +77,14 @@ source.cancel('Operation canceled by the user.');
71
77
You can also create a cancel token by passing an executor function to the ` CancelToken ` constructor:
72
78
73
79
``` js
74
- const CancelToken = this .$axios . CancelToken ;
80
+ const { CancelToken } = this .$axios ;
75
81
let cancel;
76
82
77
83
this .$axios .$get (' /user/12345' , {
78
- cancelToken: new CancelToken (function executor ( c ) {
84
+ cancelToken: new CancelToken (c => {
79
85
// An executor function receives a cancel function as a parameter
80
86
cancel = c;
81
- })
87
+ }),
82
88
});
83
89
84
90
// cancel the request
0 commit comments