英文官方文檔原文: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.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.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.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.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