今日来看一下axios的拦截器,这样在之后的外围Axios构造函数中就更容易了解了.
拦截器也是一个构造函数,而后在原型prototype上加了三个办法,别离是use, eject, forEach
use办法是增加拦截器的,eject是移除拦截器的,forEach则是迭代拦截器.具体请看https://www.kancloud.cn/yunye... 搜寻"拦截器"即可
'use strict';var utils = require('./../utils');function InterceptorManager() { this.handlers = [];}/** * Add a new interceptor to the stack * * 在栈中增加一个新的拦截器 * * @param {Function} fulfilled The function to handle `then` for a `Promise` 解决then办法的函数 * @param {Function} rejected The function to handle `reject` for a `Promise` 解决reject办法的函数 * * @return {Number} An ID used to remove interceptor later 用来移除拦截器的id */InterceptorManager.prototype.use = function use(fulfilled, rejected, options) { this.handlers.push({ fulfilled: fulfilled, rejected: rejected, synchronous: options ? options.synchronous : false, runWhen: options ? options.runWhen : null }); return this.handlers.length - 1;};/** * Remove an interceptor from the stack * * 从栈中移除拦截器 * * @param {Number} id The ID that was returned by `use` 下面的use办法返回的id */InterceptorManager.prototype.eject = function eject(id) { if (this.handlers[id]) { this.handlers[id] = null; }};/** * Iterate over all the registered interceptors 迭代所有注册的拦截器 * * This method is particularly useful for skipping over any * interceptors that may have become `null` calling `eject`. * * 这个办法在跳过曾经是null的拦截器时,特地有用 * * @param {Function} fn The function to call for each interceptor 每个拦截器都被调用的函数 */InterceptorManager.prototype.forEach = function forEach(fn) { utils.forEach(this.handlers, function forEachHandler(h) { // utils文件中的forEach办法, 参考axios源码(三) if (h !== null) { fn(h); } });};module.exports = InterceptorManager;