完整代碼以下:數組
// 定義三個狀態 const PENDING = 'pending'; // 等待 const FULFILLED = 'fulfilled'; // 成功 const REJECTED = 'rejected'; // 失敗 class MyPromise { constructor(executor) { try { executor(this.resolve, this.reject); } catch (e) { this.reject(e) } } // 定義一個表示狀態的屬性 status = PENDING; // 定義兩個實例屬性,表示成功以後的值和失敗後的緣由 value = undefined; reason = undefined; // 成功回調 successCallback = []; // 失敗回調 failedCallback = []; // 兩個屬性,這裏定義成箭頭函數,是由於咱們在使用的時候是直接調用, // 而普通函數內部this的指向window或者是undefined,定義成箭頭函數使函數內部this指向指向類實例對象 resolve = value => { // 若是狀態不是等待,阻止程序向下進行 if (this.status !== PENDING) return // 更改狀態爲成功 this.status = FULFILLED // 保存成功的值 this.value = value; // 判斷成功回調是否存在,若是存在就調用 // this.successCallback && this.successCallback(this.value); while (this.successCallback.length) { this.successCallback.shift()(); } } reject = reason => { // 若是狀態不是等待,阻止程序向下進行 if (this.status !== PENDING) return // 更改狀態爲失敗 this.status = REJECTED // 保存失敗的緣由 this.reason = reason; // 判斷失敗回調是否存在,存在就調用 // this.failedCallback && this.failedCallback(this.reason); while (this.failedCallback.length) { this.failedCallback.shift()(); } } then(successCallback, failedCallback) { // 可選參數 successCallback = successCallback ? successCallback : value => value; failedCallback = failedCallback ? failedCallback : reason => { throw reason }; let promise2 = new Promise((resolve, reject) => { // 狀態判斷 if (this.status === FULFILLED) { setTimeout(() => { try { // 定義成功回調返回值,傳給下一個then的成功回調 let successRtn = successCallback(this.value); // 判斷 x 的值是普通值仍是promise對象 // 若是是普通值 直接調用resolve // 若是是promise對象 查看promsie對象返回的結果 // 再根據promise對象返回的結果 決定調用resolve 仍是調用reject // 執行resolve方法,至關於把返回值傳遞給下一個then的成功回調函數 resolvePromise(promise2, successRtn, resolve, reject); } catch (e) { reject(e); } }, 0); } else if (this.status === REJECTED) { setTimeout(() => { try { let failedRtn = failedCallback(this.reason); resolvePromise(promise2, failedRtn, resolve, reject); } catch (e) { reject(e) } }, 0); } else { // 等待,須要將成功回調和失敗回調存儲起來,等待須要執行的時候才執行 this.successCallback.push(() => { setTimeout(() => { try { let successRtn = successCallback(this.value); resolvePromise(promise2, successRtn, resolve, reject); } catch (e) { reject(e); } }, 0); }); this.failedCallback.push(() => { setTimeout(() => { try { let failedRtn = failedCallback(this.reason); resolvePromise(promise2, failedRtn, resolve, reject); } catch (e) { reject(e) } }, 0); }); } }); return promise2; }; catch (failCallback) { this.then(undefined, failCallback) }; finally(callback) { return this.then(value => { return MyPromise.resolve(callback()).then(() => value); }, reason => { return MyPromise.resolve(callback()).then(() => { throw reason }) }) }; static all(array) { let result = []; // 用於判斷當前執行值是否等於數組長度,相等時才執行resolve() let idx = 0; return new MyPromise((resolve, reject) => { // 添加元素方法 function addElement(index, value) { result[index] = value; idx++; if (idx === array.length) { resolve(result) } } for (let i = 0; i < array.length; ++i) { let cur = array[i]; // 判斷cur是不是普通值,普通值直接進入result數組, // promise對象就執行它,調用then方法,執行成功則添加到result數組,若是失敗 if (cur instanceof MyPromise) { // promise對象 cur.then(value => addElement(i, value), reason => reject(reason)) } else { // 普通值 addElement(i, array[i]); } } }) } static race(array) { return new MyPromise((resolve, reject) => { for (let i = 0; i < array.length; ++i) { let cur = array[i]; if (cur instanceof MyPromise) { cur.then(resolve, reject); } else { resolve(cur) } } }) } static resolve(e) { if (e instanceof MyPromise) { return e; } else { return new MyPromise(resolve => resolve(e)) } } } function resolvePromise(promise2, e, resolve, reject) { if (promise2 === e) { return reject(new TypeError("Chaining cycle detected for promise #<Promise>")) } if (e instanceof MyPromise) { // promise對象 // e.then((value) => { // resolve(value) // }, (reason) => { // reject(reason) // }) // 簡化代碼 e.then(resolve, reject); } else { //普通值 resolve(e); } } module.exports = MyPromise;