切入點從場景描述出發,即先定義好咱們要實現的功能javascript
構造函數入參 executor 自執行函數。會在在 new 的時候同步執行,傳入 resolve 和 reject 狀態扭轉函數。自執行函數內部根據異步任務執行結果(成功或失敗)調用狀態扭轉函數,把狀態傳遞給後續的 then。java
promise 有三種狀態,默認是 pending,成功以後爲 fulfilled,失敗以後爲 failed。且狀態一旦發生改變,以後不可再改變也不可逆promise
then 方法接受兩個函數,表示成功、失敗狀態下的回調函數。回調函數中 return 中值會看成參數傳給後續的then。以此實現鏈式調用。異步
判斷一個變量是否爲 promise 須要兩個條件,首先要是對象,而後對象上有 then 方法,以此得出 then 執行的結果也是一個 promise函數
class Promise { constructor(executor){ this.status = 'pending', this.value = undefined; this.reason = undefined; this.onFulfilled =undefined; this.onRejected = undefined; this.onFulfilledList = []; this.onRejectedList = []; try { executor(this.resolve, this.reject); } catch(e) { this.reject(e) } } resolve(value){ if(this.status == 'pending') { this.status = 'fullfilled'; this.value = value; this.onFulfilledList.forEach(item => item()) } } reject(reason){ if(this.status == 'pending') { this.status = 'rejected'; this.reason = reason; this.onRejectedList.forEach(item => item()) } } then(onFulfilled, onRejected){ // then 以後要返回一個新的 promise let result; if(this.status == 'fullfilled' && onFulfilled) { result = onFulfilled(this.value) return Promise.resolve(result); } if(this.status == 'rejected' && onRejected) { result = onRejected(this.reason); return Promise.resolve(result); } if(this.status == 'pending') { onFulfilled && this.onFulfilledList.push(()=>onFulfilled(this.value)); onRejected && this.onRejectedList.push(() => onRejected(this.reason)); } } } Promise.resolve = function(value) { if(typeof value == 'object' && value.then) { return value; } else { return new Promise((resolve, reject)=>{ resolve(value) }) } } Promise.all = function(list) { return new Promise((resolve,reject) => { let result = []; let count = 0; for(let i = 0; i < list.length; i++) { if(typeof list[i] == 'object' && list[i].then) { Promise.resolve(list[i]).then(data =>{ result[i] = data; count++; },reject) }else { result[i] = list[i]; count++; } } if(count == list.length) { resolve(result); } }) } // Promise.race 同理,只要有一個成功,所有 resolve // Promise.finally 無論成功失敗,傳遞的回調函數都會執行,執行以後仍然能夠跟then
(完)this