Promises A+规范原文解读 + es6实现(附详细注释)

英文官方文档原文:promisesaplus.com/git

前言github

写本文的目的,是为了更好的理解promise,经过解读翻译原文,逐行解析原文经过代码一行一行实现。但愿经过这篇文章,让咱们能对promise有更深刻的了解。算法

首先介绍promises是什么,英文的字面意思是“承诺”的意思,接下来promises翻译我没有用承诺翻译这个单词,由于我以为有些英文只是一个词汇,仍是直接用英文原文叫法好。数组

promise的是解决回调的问题的,经过then的链式调用,让咱们能更清晰的理解阅读代码。下面直接看原文解读:promise

英: An open standard for sound, interoperable JavaScript promises—by implementers, for implementers.缓存

中: 一个开源标准,可与JS互操做的Promises。由 implementers(语意创做这个promises的人或团体)创做,implementers(实现者)。bash

英: A promise represents the eventual result of an asynchronous operation. The primary way of interacting with a promise is through its then method, which registers callbacks to receive either a promise’s eventual value or the reason why the promise cannot be fulfilled.异步

中: 一个promise表明了异步操做的最终结果。与一个promise完成交互的主要方法是then方法,该方法会记录回调以接收一个promise的成功返回值或者失败的缘由。async

英: This specification details the behavior of the then method, providing an interoperable base which all Promises/A+ conformant promise implementations can be depended on to provide. As such, the specification should be considered very stable. Although the Promises/A+ organization may occasionally revise this specification with minor backward-compatible changes to address newly-discovered corner cases, we will integrate large or backward-incompatible changes only after careful consideration, discussion, and testing.ide

中: 本规范详细的描述了then方法的行为,提供了一个可互操做的基础,全部的Promises / A +符合promise实现均可依赖提供。所以,本规范应该被认为是很是稳定的。虽然Promises / A +组织偶尔会修改这个规范,但向后兼容更改次数较少,主要解决新发现的案例,咱们只有在仔细考虑,讨论以及测试以后才会整合大的改动或者向后兼容的更改。

英: Historically, Promises/A+ clarifies the behavioral clauses of the earlier Promises/A proposal, extending it to cover de facto behaviors and omitting parts that are underspecified or problematic.

中: 历史上,Promises / A +阐明了先前Promises / A提案的行为条款,将其扩展为涵盖事实上的行为,并略去了未指定或存在问题的部分。

英: Finally, the core Promises/A+ specification does not deal with how to create, fulfill, or reject promises, choosing instead to focus on providing an interoperable then method. Future work in companion specifications may touch on these subjects.

中: 最后,核心Promises / A+规范不涉及如何建立,履行或拒绝promises,而是选择专一于提供可互操做的方法。将来的配套规范工做可能涉及这些主题。

英: 1.Terminology

中: 一、术语

英: 1.1 “promise” is an object or function with a then method whose behavior conforms to this specification.

中: 1.1 “promise”是一个对象或函数,它的行为符合这个规范。

英: 1.2 “thenable” is an object or function that defines a then method.

中: 1.2 “thenable”是定义then方法的对象或函数。

英: 1.3 “value” is any legal JavaScript value (including undefined, a thenable, or a promise).

中: 1.3 “value”是任何合法的JavaScript值(包括undefined,thenable或promise)。

英: 1.4 “exception” is a value that is thrown using the throw statement.

中: 1.4 “异常”是使用throw语句抛出的值。

英: 1.5 “reason” is a value that indicates why a promise was rejected.

中: 1.5 “缘由”是一个值(结果)代表promise被拒绝的缘由。

英: 2.Requirements

2.1.Promise States
复制代码

中: 二、要求

2.1.Promise状态
复制代码

英: A promise must be in one of three states: pending, fulfilled, or rejected.

中: 一个promise必须包含初始态, 成功(完成)态, 或者失败(拒绝)态这三个状态中的一种。

英: 2.1.1 When pending, a promise:
2.1.1.1 may transition to either the fulfilled or rejected state.

中: 2.1.1 当状态是初始态, promise:
2.1.1.1 可能转换到成功态或失败态。

英: 2.1.2. When fulfilled, a promise:
2.1.2.1. must not transition to any other state.
2.1.2.2. must have a value, which must not change.

中: 2.1.2 当状态是成功态,promise:
2.1.2.1 不能更改为别的状态。
2.1.2.2 必须有个不能更改的值(结果)

英: 2.1.3. When rejected, a promise:
2.1.3.1.must not transition to any other state.
2.1.3.2. must have a reason, which must not change.

中: 2.1.3 当状态是失败态,promise:
2.1.3.1. 不能更改为别的状态。
2.1.3.2. 必须有个不能更改的失败(错误)缘由

英: Here, “must not change” means immutable identity (i.e. ===), but does not imply deep immutability.

中: 上面,“不能改变”的意思是不可改变的状态(即 ===),但并不意味着深不可变。

英: 2.2.The then Method
A promise must provide a then method to access its current or eventual value or reason.
A promise’s then method accepts two arguments:
promise.then(onFulfilled, onRejected)

中: 2.2 then方法
一个promise必须有一个then方法来获取成功的值(结果)或失败(错误)的缘由。
一个promise方法接收两个参数:
promise.then(onFulfilled, onRejected)

英: 2.2.1. Both onFulfilled and onRejected are optional arguments:
2.2.1.1.If onFulfilled is not a function, it must be ignored.
2.2.1.2.If onRejected is not a function, it must be ignored.

中: 2.2.1. onFulfilled和onRejected都是可选参数:
2.2.1.1 若是onFulfilled不是函数,则必须忽略它。
2.2.1.2 若是onRejected不是函数,则必须忽略它。

英: 2.2.2 If onFulfilled is a function:
2.2.2.1.it must be called after promise is fulfilled, with promise’s value as its first argument.
2.2.2.2.it must not be called before promise is fulfilled.
2.2.2.3.it must not be called more than once.

中: 2.2.2 若是onFulfilled是一个函数:
2.2.2.1 必须在promise执行完成后调用,promise的返回值做为第一个参数。
2.2.2.2 在promise执行前不得调用。
2.2.2.3 只能调用一次。

英: 2.2.3.If onRejected is a function,
2.2.3.1.it must be called after promise is rejected, with promise’s reason as its first argument.
2.2.3.2.it must not be called before promise is rejected.
2.2.3.3.it must not be called more than once.

中: 2.2.3 若是onRejected是一个函数:
2.2.3.1 必须在promise执行完成后调用,promise的错误缘由做为第一个参数。
2.2.3.2 在promise执行前不得调用。
2.2.3.3 只能调用一次。

英: 2.2.4.onFulfilled or onRejected must not be called until the execution context stack contains only platform code. [3.1].

中: 2.2.4 在执行上下文堆栈仅包含平台代码以前,不能调用onFulfilled或onRejected。[3.1]。

英: 2.2.5 onFulfilled and onRejected must be called as functions (i.e. with no this value). [3.2]

中: onFulfilled和onRejected必须是函数(即 没有这个值)。[3.2]

英: 2.2.6.then may be called multiple times on the same promise.
2.2.6.1.If/when promise is fulfilled, all respective onFulfilled callbacks must execute in the order of their originating calls to then.
2.2.6.2.If/when promise is rejected, all respective onRejected callbacks must execute in the order of their originating calls to then.

中: 2.2.6 then方法可能会在相同的promise被屡次调用。
2.2.6.1 若是/当promise成功时,全部各自的onFulfilled回调必须按照其始发调用的顺序执行。
2.2.6.2 若是/当promise失败时,全部各自的onRejected回调必须按照其始发调用的顺序执行。

英: 2.2.7. then must return a promise [3.3].
promise2 = promise1.then(onFulfilled, onRejected);
2.2.7.1.If either onFulfilled or onRejected returns a value x, run the Promise Resolution Procedure [[Resolve]](promise2, x).
2.2.7.2.If either onFulfilled or onRejected throws an exception e, promise2 must be rejected with e as the reason.
2.2.7.3.If onFulfilled is not a function and promise1 is fulfilled, promise2 must be fulfilled with the same value as promise1.
2.2.7.4.If onRejected is not a function and promise1 is rejected, promise2 must be rejected with the same reason as promise1.

中: 2.2.7 then方法必须返回一个promise。[3.3]
例如:promise2 = promise1.then(onFulfilled, onRejected);
2.2.7.1 若是onFulfilled或onRejected返回一个值(结果)x,运行Promise的解决程序 [[Resolve]](promise2,x)。
2.2.7.2 若是onFulfilled或onRejected引起异常e,promise2必须以e做为拒绝缘由。
2.2.7.3 若是onFulfilled不是一个函数而且promise1是被成功,那么promise2必须用与promise1相同的值执行。
2.2.7.4 若是onRejected不是函数而且promise1是被失败,那么promise2必须用与promise1相同的失败缘由。

英: 2.3.The Promise Resolution Procedure

中: 2.3 Promise的解决程序

英: The promise resolution procedure is an abstract operation taking as input a promise and a value, which we denote as [[Resolve]](promise, x). If x is a thenable, it attempts to make promise adopt the state of x, under the assumption that x behaves at least somewhat like a promise. Otherwise, it fulfills promise with the value x.

中: Promise的解决程序是一个抽象操做,取得输入的promise和一个值(结果),咱们表示为 [[Resolve]](promise, x)。 [[Resolve]](promise, x)的意思是建立一个方法Resolve方法执行时传入两个参数promise和x(promise成功态时返回的值)。若是x是一个thenable(见上文术语1.2),它试图创造一个promise采用x的状态,假设x的行为至少貌似promise。不然,它用x值执行promise。

英: This treatment of thenables allows promise implementations to interoperate, as long as they expose a Promises/A+-compliant then method. It also allows Promises/A+ implementations to “assimilate” nonconformant implementations with reasonable then methods.

中: 对thenable的这种处理容许promise实现交互操做,只要它们暴露Promise / A +兼容的方法便可。 它还容许Promises / A +实现经过合理的方法“吸取”不合格的实现。

英: To run [[Resolve]](promise, x), perform the following steps:
2.3.1. if promise and x refer to the same object, reject promise with a TypeError as the reason.

中: 去运行 [[Resolve]](promise, x),需执行如下步骤:
2.3.1 若是promise和x引用同一个对象,则以TypeError为缘由拒绝promise。

英: 2.3.2. If x is a promise, adopt its state [3.4]:
2.3.2.1.If x is pending, promise must remain pending until x is fulfilled or rejected.
2.3.2.2.If/when x is fulfilled, fulfill promise with the same value.
2.3.2.3.If/when x is rejected, reject promise with the same reason.

中: 2.3.2 若是x是一个promise,采用它的状态【3.4】:
2.3.2.1 若是x是初始态,promise必须保持初始态(即递归执行这个解决程序),直到x被成功或被失败。(即,直到resolve或者reject执行)
2.3.2.2 若是/当x被成功时,用相同的值(结果)履行promise。
2.3.2.3 若是/当x被失败时,用相同的错误缘由履行promise。

英: 2.3.3.Otherwise, if x is an object or function,
2.3.3.1.Let then be x.then. [3.5]
2.3.3.2.If retrieving the property x.then results in a thrown exception e, reject promise with e as the reason.
2.3.3.3.If then is a function, call it with x as this, first argument resolvePromise, and second argument rejectPromise, where:
2.3.3.3.1.If/when resolvePromise is called with a value y, run [[Resolve]](promise, y).
2.3.3.3.2.If/when rejectPromise is called with a reason r, reject promise with r.
2.3.3.3.3.If both resolvePromise and rejectPromise are called, or multiple calls to the same argument are made, the first call takes precedence, and any further calls are ignored.
2.3.3.3.4.If calling then throws an exception e,
2.3.3.3.4.1.If resolvePromise or rejectPromise have been called, ignore it.
2.3.3.3.4.2.Otherwise, reject promise with e as the reason.
2.3.3.4.If then is not a function, fulfill promise with x.

中: 2.3.3 不然,若是x是一个对象或函数,
2.3.3.1 让then等于x.then。【3.5】
2.3.3.2 若是x.then致使抛出异常e,拒绝promise并用e做为失败缘由。
2.3.3.3 若是then是一个函数,则使用x做为此参数调用它,第一个参数resolvePromise,第二个参数rejectPromise,其中:
2.3.3.3.1 若是使用值(结果)y调用resolvePromise,运行[[Resolve]](promise,y)个人解决程序的名字是resolveExecutor。
2.3.3.3.2 若是使用拒绝缘由r调用resolvePromise,运行reject(r)。
2.3.3.3.3 若是resolvePromise和rejectPromise都被调用,或者对同一个参数进行屡次调用,则第一次调用优先,而且任何进一步的调用都会被忽略。
2.3.3.3.4 若是调用then方法抛出异常e,
2.3.3.3.4.1 若是resolvePromise或rejectPromise已经调用了,则忽略它。
2.3.3.3.4.2 不然,以e做为失败缘由拒绝promise。
2.3.3.4 若是then不是一个对象或者函数,则用x做为值(结果)履行promise。

英: 2.3.4.If x is not an object or function, fulfill promise with x.
If a promise is resolved with a thenable that participates in a circular thenable chain, such that the recursive nature of [[Resolve]](promise, thenable) eventually causes [[Resolve]](promise, thenable) to be called again, following the above algorithm will lead to infinite recursion. Implementations are encouraged, but not required, to detect such recursion and reject promise with an informative TypeError as the reason. [3.6]

中: 2.3.4 若是x不是一个对象或函数,则用x做为值履行promise。
若是一个primse是经过一个thenable参与一个循环的可连接表达式来解决的thenable链,那么[[Resolve]](promise,thenable)的递归性质最终会致使[[Resolve]](promise,thenable)被再次调用, 上述算法将致使无限递归。 支持这种实现,但不是必需的,来检测这种递归并以一个信息性的TypeError为理由拒绝promise。【3.6】

英: 3.Notes

3.1.Here “platform code” means engine, environment, and promise implementation code. In practice, this requirement ensures that onFulfilled and onRejected execute asynchronously, after the event loop turn in which then is called, and with a fresh stack. This can be implemented with either a “macro-task” mechanism such as setTimeout or setImmediate, or with a “micro-task” mechanism such as MutationObserver or process.nextTick. Since the promise implementation is considered platform code, it may itself contain a task-scheduling queue or “trampoline” in which the handlers are called.
复制代码

中: 3.注释

3.1 这里的“平台代码”是指引擎,环境和primise实现代码。在实践中,这个要求确保onFulfilled和onRejected异步执行,在事件循环开始以后then被调用,和一个新的堆栈。这可使用诸如setTimeout或setImmediate之类的“宏任务”机制,或者使用诸如MutationObserver或process.nextTick的“微任务”机制来实现。因为promise实现被认为是通过深思熟虑的平台代码,所以它自己可能包含调用处理程序的任务调度队列或或称为“trampoline”(可重用的)的处理程序。
复制代码

英: 3.2.That is, in strict mode this will be undefined inside of them; in sloppy mode, it will be the global object.

中: 3.2 即:在严格模式下是undefined;非严格模式下是全局对象。

英: 3.3.Implementations may allow promise2 === promise1, provided the implementation meets all requirements. Each implementation should document whether it can produce promise2 === promise1 and under what conditions.

中: 3.3 实现可能会容许promise2 === promise1, 前提是实现符合全部的要求。每一个实现应该记录它是否能够产生promise2 === promise1以及在什么条件下。

英: 3.4.Generally, it will only be known that x is a true promise if it comes from the current implementation. This clause allows the use of implementation-specific means to adopt the state of known-conformant promises.

中: 3.4 一般,只有当它是来自当前的实现时才会知道x是一个真正的promise。该条款容许使用具体实现方法来采用已知符合性promise的状态。

英: 3.5.This procedure of first storing a reference to x.then, then testing that reference, and then calling that reference, avoids multiple accesses to the x.then property. Such precautions are important for ensuring consistency in the face of an accessor property, whose value could change between retrievals.

中: 3.5 首先存储对x.then的引用,而后测试和调用该引用,避免屡次使用x.then属性。这样的注意事项对于确保访问器的属的一致性性很是重要,其值(结果)可能在取回时改变。

英: 3.6.Implementations should not set arbitrary limits on the depth of thenable chains, and assume that beyond that arbitrary limit the recursion will be infinite. Only true cycles should lead to a TypeError; if an infinite chain of distinct thenables is encountered, recursing forever is the correct behavior.

中: 3.6 实现不该该对thenable链作任意设定限制,假定超出该任意设定限制递归将是无限的。只有真正的循环才致使TypeError; 若是遇到不一样的thenables的无限链,一直递归是正确的行为。

英: To the extent possible under law, the Promises/A+ organization has waived all copyright and related or neighboring rights to Promises/A+ Promise Specification. This work is published from: United States.

中: 在法律容许的范围内,Promises / A +组织已放弃全部版权及Promises / A +规范的相关或相邻权利。本做品发表于:美国。

---------以上是原文翻译

啥也别说上代码

class MyPromise {
  constructor(executor) {
    //缓存this
    ;let self = this
    //设置初始态
    ;self.status = 'pending'
    //定义成功的值默认undefined
    ;self.value = undefined
    //定义失败的缘由默认undefined
    ;self.reason = undefined
    //定义成功的回调数组
    ;self.onResolvedCallbacks = []
    //定义失败的回调数组
    ;self.onRejectedCallbacks = []
    //定义成功时执行的函数
    ;let resolve = value => {
      ;if (value instanceof MyPromise) return value.then(resolve, reject)
      //异步执行成功回调
      ;setTimeout(() => {
        if (self.status === 'pending') {
          //把状态改成成功态
          ;self.status = 'fulfilled'
          //保存成功的值
          ;self.value = value
          //遍历执行每一个成功的回调
          ;self.onResolvedCallbacks.forEach(onFulfilled => onFulfilled(value))
        }
      })
    }
    //定义失败时执行的函数
    ;let reject = reason => {
      //异步执行失败回调
      setTimeout(() => {
        if (self.status === 'pending') {
          //把状态改成失败态
          ;self.status = 'rejected'
          //保存失败的缘由
          ;self.reason = reason
          //遍历执行每一个失败的回调
          ;self.onRejectedCallbacks.forEach(onRejected => onRejected(reason))
        }
      })
    }
    //因为调用executor这个方法有可能异常,须要将捕获的异常reject出去
    ;try {
      //运行传进来的函数把成功和失败的方法传进去
      ;executor(resolve, reject)
    } catch (e) {
      ;reject(e)
    }
  }
  /**
   * @param {value} onFulfilled //值的穿透,默认值日后传
   * @param {reason} onRejected //默认把失败缘由日后抛
   */
  then(onFulfilled = value => value, onRejected = reason => {throw reason}) {
    //缓存this,定义promise2
    ;let self = this
    ;let promise2
    //promise主要解决程序,也是promise的难点
    ;let resolveExecutor = (promise2, x, resolve, reject) => {
      // 定义个标识 promise2是否已经resolve 或 reject了
      ;let isThenCalled = false
      // 2.3.1 若是promise和x引用同一个对象,则以TypeError为缘由拒绝promise。
      ;if (promise2 === x) return reject(new TypeError('循环引用!!!'))
      // 2.3.2 若是x是一个promise,采用它的状态【3.4
      ;if (x instanceof MyPromise) {
        /**
         * 2.3.2.1 若是x是初始态,promise必须保持初始态(即递归执行这个解决程序),直到x被成功或被失败。(即,直到resolve或者reject执行)
         */
        if (x.status === 'pending') {
          x.then(function(y) {
            ;resolveExecutor(promise2, y, resolve, reject)
          }, reject)
        } else {
          // 2.3.2.2 若是/当x被成功时,用相同的值(结果)履行promise。
          // 2.3.2.3 若是/当x被失败时,用相同的错误缘由履行promise。
          ;x.then(resolve, reject)
        }
      } else if (x !== null && (typeof x === 'object' || typeof x === 'function')) {
        // 2.3.3 不然,若是x是一个对象或函数,
        try {
          // 2.3.3.1 让then等于x.then。【3.5】
          ;let then = x.then
          if (typeof then === 'function') {
            //2.3.3.3.3 若是resolvePromise和rejectPromise都被调用,或者对同一个参数进行屡次调用,则第一次调用优先,而且任何进一步的调用都会被忽略。
            ;let resolvePromise = y => {
              //若是promise2已经成功或失败了,就return掉
              ;if (isThenCalled) return
              ;isThenCalled = true
              //2.3.3.3.1 若是使用值(结果)y调用resolvePromise,运行[[Resolve]](promise,y)个人解决程序的名字是resolveExecutor,也就是递归调用。
              ;resolveExecutor(promise2, y, resolve, reject)
            }
            ;let rejectPromise = r => {
              //若是promise2已经成功或失败了,就return掉
              ;if (isThenCalled) return
              ;isThenCalled = true
              //2.3.3.3.2 若是使用拒绝缘由r调用resolvePromise,运行reject(r)。
              ;reject(r)
            };
            // 2.3.3.3 若是then是一个函数,则使用x做为此参数调用它,第一个参数resolveExecutor,第二个参数rejectPromise,其中
            ;then.call(x, resolvePromise, rejectPromise)
          } else {
            //到此的话x不是一个thenable对象,那直接把它当成值resolve promise2就能够了
            ;resolve(x)
          }
        } catch (e) {
          //2.3.3.3.4 若是调用then方法抛出异常e,
          //2.3.3.3.4.1 若是resolvePromise或rejectPromise已经调用了,则忽略它。
          ;if (isThenCalled) return
          ;isThenCalled = true

          //2.3.3.2 若是x.then致使抛出异常e,拒绝promise并用e做为失败缘由
          //2.3.3.3.4.2 不然,以e做为失败缘由拒绝promise
          ;reject(e)
        }
      } else {
        //2.3.3.4 若是then不是一个对象或者函数,则用x做为值(结果)履行promise。
        ;resolve(x)
      }
    };
    if (self.status === 'fulfilled') {
      //2.2.7
      return (promise2 = new MyPromise((resolve, reject) => {
        //2.2.4 在执行上下文堆栈仅包含平台代码以前,不能调用onFulfilled或onRejected。[3.1]。
        //3.1 这里的“平台代码”是指引擎,环境和primise实现代码。在实践中,这个要求确保onFulfilled和onRejected异步执行,在事件循环开始以后then被调用,和一个新的堆栈。这可使用诸如setTimeout或setImmediate之类的“宏任务”机制,或者使用诸如MutationObserver或process.nextTick的“微任务”机制来实现。因为promise实现被认为是通过深思熟虑的平台代码,所以它自己可能包含调用处理程序的任务调度队列或或称为“trampoline”(可重用的)的处理程序。
        //让onFulfilled异步执行
        setTimeout(() => {
          try {
            ;let x = onFulfilled(self.value)
            ;resolveExecutor(promise2, x, resolve, reject)
          } catch (e) {
            ;reject(e)
          }
        })
      }))
    }
    if (self.status === 'rejected') {
      return (promise2 = new MyPromise((resolve, reject) => {
        //2.2.4 在执行上下文堆栈仅包含平台代码以前,不能调用onFulfilled或onRejected。[3.1]。
        //3.1 这里的“平台代码”是指引擎,环境和primise实现代码。在实践中,这个要求确保onFulfilled和onRejected异步执行,在事件循环开始以后then被调用,和一个新的堆栈。这可使用诸如setTimeout或setImmediate之类的“宏任务”机制,或者使用诸如MutationObserver或process.nextTick的“微任务”机制来实现。因为promise实现被认为是通过深思熟虑的平台代码,所以它自己可能包含调用处理程序的任务调度队列或或称为“trampoline”(可重用的)的处理程序。
        //让onFulfilled异步执行
        setTimeout(() => {
          try {
            ;let x = onRejected(self.reason)
            ;resolveExecutor(promise2, x, resolve, reject)
          } catch (e) {
            ;reject(e)
          }
        })
      }))
    }
    if (self.status === 'pending') {
      return (promise2 = new MyPromise((resolve, reject) => {
        self.onResolvedCallbacks.push(() => {
          try {
            ;let x = onFulfilled(self.value)
            ;resolveExecutor(promise2, x, resolve, reject)
          } catch (e) {
            ;reject(e)
          }
        });
        self.onRejectedCallbacks.push(() => {
          try {
            ;let x = onRejected(self.reason)
            ;resolveExecutor(promise2, x, resolve, reject)
          } catch (e) {
            ;reject(e)
          }
        })
      }))
    }
  }
  catch(onRejected) {
    ;this.then(null, onRejected)
  }
  //当即成功的promise
static resolve(value) {
    return new MyPromise(resolve => {
      ;resolve(value)
    })
  }
  //当即失败的promise
static reject(reason) {
    return new MyPromise((resolve, reject) => {
      ;reject(reason)
    })
  }
  //promise all方法,只要有一个失败就失败了。
static all(promises) {
    return new MyPromise((resolve, reject) => {
      ;let len = promises.length
      ;let resolveAry = []
      ;let count = 0
      for (let i = 0; i < len; i++) {
        promises[i].then(value => {
          ;resolveAry[i] = value
          ;if (++count === len) resolve(resolveAry)
        }, reject)
      }
    })
  }
  //promise race方法,看resolve和reject哪一个先返回,就取哪一个值,成功就取成功的value,失败就取失败的reason。
  static race(promises) {
    return new MyPromise((resolve, reject) => {
      for (let i = 0, l = promises.length; i < l; i++) {
        ;promises[i].then(resolve, reject)
      }
    })
  }
}
module.exports = MyPromise;
复制代码

~~~ 若有不妥之处,欢迎你们留言指正,如以为好请关注,点赞,转发请注明出处,谢谢! ~~~

邮箱:gameness1212@163.com

相关文章
相关标签/搜索