我在知乎上看到这样一个。axios 封装 取消请求函数。我写的只是大概思路。 有没有 jy 看到过类似的。
```javascript
function withCancel(fetcher) {
let cancelSource = null
const CancelToken = axios.CancelToken;
cancelSource = CancelToken.source();
const getData = () => {
cancel();
fetcher();
}
const cancel = () => {
if (cancelSource) {
cancelSource.cancel()
}
}
return [getData, cancel];
}
```
```javascript
function withCancel(fetcher) {
let cancelSource = null
const CancelToken = axios.CancelToken;
cancelSource = CancelToken.source();
const getData = () => {
cancel();
fetcher();
}
const cancel = () => {
if (cancelSource) {
cancelSource.cancel()
}
}
return [getData, cancel];
}
```
展开
6
4