We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第405天 axios拦截器原理是什么?
3+1官网
我也要出题
The text was updated successfully, but these errors were encountered:
拦截器原理其实就是用use添加用户自定义的函数到拦截器的数组中。 最后把他们放在拦截器请求前,请求后。组成promise链式调用。
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;
更具体的可以看我的这篇源码文章。 @若川:学习 axios 源码整体架构,打造属于自己的请求库
Sorry, something went wrong.
开发者用use注册的拦截器回调函数,包括成功回调和失败回调,会成对的推入chain数组,请求拦截器的回调在前,dispatchRequest在中间,响应拦截器的回调在后,然后通过promise的链式调用,将chain中的回调成对注册为微任务异步队列中去,这样在执行异步队列时,就会先执行请求拦截器的回调,再执行dispatchRequest发起xhr请求,有了结果后再执行响应拦截器的回调。
No branches or pull requests
第405天 axios拦截器原理是什么?
3+1官网
我也要出题
The text was updated successfully, but these errors were encountered: