function Axios(config) {
this.default = config;
this.interceptors = {
request: new InterceptorManager(),
response: new InterceptorManager(),
}
}
Axios.prototype.request = function(config) {
let promise = Promise.resolve(config);
var chains = [dispatchRequest, undefined];
// 将拦截器回调返回值压入chain
this.interceptors.request.handles.forEach(item => {
chains.unshift(item.fulfilled,item.rejected)
})
this.interceptors.response.handles.forEach(item => {
chains.push(item.fulfilled,item.rejected)
})
while(chains.length) {
promise = promise.then(chains.shift(), chains.shift());
}
return promise
}
function dispatchRequest(config) {
return xhrAdapter(config).then(response &
手写axios实现请求响应拦截器和中断请求
最新推荐文章于 2024-06-14 20:14:28 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)