Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

Axios interceptor 这段 ForEach 函数有什么用?

如下,参数是函数类型的 fn,当条件成立时执行 fn(interceptor),函数是什么功能 ?

interface ResolvedFn<T> {
    (val: T): T | Promise<T>
}

interface RejectedFn {
    (error: any): any
}

interface Interceptor<T> {
    resolved: ResolvedFn<T>
    rejected?: RejectedFn
}

export default class InterceptorManager<T> {
  private interceptors: Array<Interceptor<T> | null>

  constructor() {
      this.interceptors = []
  }

  use(resolved: ResolvedFn<T>, rejected?: RejectedFn): number {
      this.interceptors.push({
          resolved,
          rejected
      })
      return this.interceptors.length - 1
  }
  // 问题代码块
  forEach(fn: (interceptor: Interceptor<T>) => void): void {
      this.interceptors.forEach(interceptor => {
          if (interceptor !== null) {
              fn(interceptor)
          }
      })
  }

  eject(id: number): void {
      if (this.interceptors[id]) {
          this.interceptors[id] = null
      }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

队列,拦截器可以多处调用,每个都会入栈,然后当有ajax请求时调用


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...