英文官方文档原文:https://promisesaplus.com/
媒介
写本文的目标,是为了更好的明白promise,经由过程解读翻译原文,逐行剖析原文经由过程代码一行一行完成。愿望经由过程这篇文章,让我们能对promise有更深切的相识。
起首引见promises是什么,英文的字面意义是“许诺”的意义,接下来promises翻译我没有用许诺翻译这个单词,因为我以为有些英文只是一个辞汇,照样直接用英文原文叫法好。
promise的是处置惩罚回调的题目的,经由过程then的链式挪用,让我们能更清楚的明白浏览代码。下面直接看原文解读:
英: An open standard for sound, interoperable JavaScript promises—by implementers, for implementers.
中: 一个开源范例,可与JS互操纵的Promises。由 implementers(语意创作这个promises的人或整体)创作,implementers(完成者)。
英: 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的胜利返回值或许失利的缘由。
英: 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.
中: 本范例细致的形貌了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.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、请求
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.<br />
2.1.2.2. must have a value, which must not change.
中: 2.1.2 当状况是胜利态,promise:
2.1.2.1 不能变动成别的状况。<br />
2.1.2.2 必须有个不能变动的值(效果)
英: 2.1.3. When rejected, a promise:
2.1.3.1.must not transition to any other state.<br />
2.1.3.2. must have a reason, which must not change.
中: 2.1.3 当状况是失利态,promise:
2.1.3.1. 不能变动成别的状况。<br />
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.<br />
A promise’s then method accepts two arguments:<br />
promise.then(onFulfilled, onRejected)
中: 2.2 then要领
一个promise必须有一个then要领来猎取胜利的值(效果)或失利(毛病)的缘由。<br />
一个promise要领吸取两个参数:<br />
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.<br />
2.2.1.2.If onRejected is not a function, it must be ignored.
中: 2.2.1. onFulfilled和onRejected都是可选参数:
2.2.1.1 假如onFulfilled不是函数,则必须疏忽它。<br />
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.<br />
2.2.2.2.it must not be called before promise is fulfilled.<br />
2.2.2.3.it must not be called more than once.
中: 2.2.2 假如onFulfilled是一个函数:
2.2.2.1 必须在promise实行完成后挪用,promise的返回值作为第一个参数。<br />
2.2.2.2 在promise实行前不得挪用。<br />
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.<br />
2.2.3.2.it must not be called before promise is rejected.<br />
2.2.3.3.it must not be called more than once.
中: 2.2.3 假如onRejected是一个函数:
2.2.3.1 必须在promise实行完成后挪用,promise的毛病缘由作为第一个参数。<br />
2.2.3.2 在promise实行前不得挪用。<br />
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.<br />
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回调必须根据其始发挪用的递次实行。<br />
2.2.6.2 假如/当promise失利时,一切各自的onRejected回调必须根据其始发挪用的递次实行。<br />
英: 2.2.7. then must return a promise [3.3].
promise2 = promise1.then(onFulfilled, onRejected);<br />
2.2.7.1.If either onFulfilled or onRejected returns a value x, run the Promise Resolution Procedure [[Resolve]](promise2, x).<br />
2.2.7.2.If either onFulfilled or onRejected throws an exception e, promise2 must be rejected with e as the reason.<br />
2.2.7.3.If onFulfilled is not a function and promise1 is fulfilled, promise2 must be fulfilled with the same value as promise1.<br />
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);<br />
2.2.7.1 假如onFulfilled或onRejected返回一个值(效果)x,运转Promise的处置惩罚顺序 [[Resolve]](promise2,x)。<br />
2.2.7.2 假如onFulfilled或onRejected激发非常e,promise2必须以e作为谢绝缘由。<br />
2.2.7.3 假如onFulfilled不是一个函数而且promise1是被胜利,那末promise2必须用与promise1雷同的值实行。<br />
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.<br />
2.3.2.2.If/when x is fulfilled, fulfill promise with the same value.<br />
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实行)<br />
2.3.2.2 假如/当x被胜利时,用雷同的值(效果)推行promise。<br />
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]<br />
2.3.3.2.If retrieving the property x.then results in a thrown exception e, reject promise with e as the reason.<br />
2.3.3.3.If then is a function, call it with x as this, first argument resolvePromise, and second argument rejectPromise, where:<br />
2.3.3.3.1.If/when resolvePromise is called with a value y, run [[Resolve]](promise, y).<br />
2.3.3.3.2.If/when rejectPromise is called with a reason r, reject promise with r.<br />
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.<br />
2.3.3.3.4.If calling then throws an exception e,<br />
2.3.3.3.4.1.If resolvePromise or rejectPromise have been called, ignore it.<br />
2.3.3.3.4.2.Otherwise, reject promise with e as the reason.<br />
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】<br />
2.3.3.2 假如x.then致使抛出非常e,谢绝promise并用e作为失利缘由。<br />
2.3.3.3 假如then是一个函数,则运用x作为此参数挪用它,第一个参数resolvePromise,第二个参数rejectPromise,个中:<br />
2.3.3.3.1 假如运用值(效果)y挪用resolvePromise,运转[[Resolve]](promise,y)我的处置惩罚顺序的名字是resolveExecutor。<br />
2.3.3.3.2 假如运用谢绝缘由r挪用resolvePromise,运转reject(r)。<br />
2.3.3.3.3 假如resolvePromise和rejectPromise都被挪用,或许对同一个参数举行屡次挪用,则第一次挪用优先,而且任何进一步的挪用都会被疏忽。<br />
2.3.3.3.4 假如挪用then要领抛出非常e,<br />
2.3.3.3.4.1 假如resolvePromise或rejectPromise已挪用了,则疏忽它。<br />
2.3.3.3.4.2 不然,以e作为失利缘由谢绝promise。<br />
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 {Function} onFulfilled //值的穿透,默许值今后传
* @param {Function} 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