运行机制

promise链

// 组成`Promise`链
// Hook up interceptors middleware
// 把 xhr 请求 的 dispatchRequest 和 undefined 放在一个数组里
var chain = [dispatchRequest, undefined];
// 创建 Promise 实例
var promise = Promise.resolve(config);

// 遍历用户设置的请求拦截器 放到数组的 chain 前面
this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
  chain.unshift(interceptor.fulfilled, interceptor.rejected);
});

// 遍历用户设置的响应拦截器 放到数组的 chain 后面
this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
  chain.push(interceptor.fulfilled, interceptor.rejected);
});

// 遍历chain 数组,直到遍历 chain.length 为 0
while (chain.length) {
  // 两两对应移出来 放到 then 的两个参数里。
  promise = promise.then(chain.shift(), chain.shift());
}

return promise;

适配器模式

取消

在各个阶段都检查有没有取消操作throwIfCancellationRequested

最后更新于

这有帮助吗?