手写axios实现请求响应拦截器和中断请求

       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 &

你可能感兴趣的:(js,前端学习,面试题,axios,promise)