function Promise(task) { let that = this;//緩存this //默認狀態爲pending that.status = 'pending'; //此變量裏放着此promise的結果 that.value = undefined; //存放的着全部成功的回調函數 that.onResolvedCallbacks = []; //存放着全部的失敗的回調函數 that.onRejectedCallbacks = []; //調用此方法能夠把promise變成成功態 //resolve的時候你把掙到的錢傳進來 function resolve(value) { if(value instanceof Promise){ return value.then(resolve,reject); } if (that.status == 'pending') { that.status = 'fulfilled'; that.value = value; that.onResolvedCallbacks.forEach(item=>item(that.value)); } } //調用此方法能夠把當前的promise變成失敗態 function reject(reason) { //若是當前狀態是初始態,則轉成失敗態 if (that.status == 'pending') { that.status = 'rejected'; that.value = reason; that.onRejectedCallbacks.forEach(item=>item(that.value)); } } //當即執行傳入的任務 try { task(resolve, reject); } catch (e) { reject(e); } } function resolvePromise(promise2,x,resolve,reject){ let then; //若是x就是promise2 if(promise2 === x){ return reject(new TypeError('循環引用')); } if(x instanceof Promise){ if(x.status == 'pending'){ x.then(function(y){ resolvePromise(promise2,y,resolve,reject); },reject); }else if(x.status == 'fulfilled'){ resolve(x.value); }else if(x.status == 'rejected'){ reject(x.value); } }else if(x!=null && (typeof x == 'object' || typeof x == 'function')){ try{ then = x.then; if(typeof then == 'function'){ then.call(x,function(y){ resolvePromise(promise2,y,resolve,reject) },reject); } }catch(e){ reject(e); }; }else{ resolve(x); } } //onFulfilled成功的回調,onReject失敗的回調 Promise.prototype.then = function (onFulfilled, onReject) { onFulfilled = typeof onFulfilled == 'function'?onFulfilled:function(value){return value}; onReject = typeof onReject=='function'?onReject:function(reason){ throw reason; } let that = this; let promise2; if(that.status == 'fulfilled'){ promise2 = new Promise(function(resolve,reject){ let x = onFulfilled(that.value); resolvePromise(promise2,x,resolve,reject); }); } if(that.status == 'rejected'){ promise2 = new Promise(function(resolve,reject){ let x = onReject(that.value); resolvePromise(promise2,x,resolve,reject); }); } if(that.status == 'pending'){ promise2 = new Promise(function(resolve,reject){ that.onResolvedCallbacks.push(function(){ let x = onFulfilled(that.value); resolvePromise(promise2,x,resolve,reject); }); that.onRejectedCallbacks.push(function(){ let x = onReject(that.value); resolvePromise(promise2,x,resolve,reject); }); }); } return promise2; } Promise.all = function (arr) { return new Promise((resolve,reject) =>{ let values = [] let len = arr.length for(var i = 0;i < len; i++){ let promise = arr[i] if(typeof promise.then == 'function'){ promise.then(res=>{ values.push(res) if(values.length == len){ resolve(values) } }) } } }) } Promise.race = function (arr) { return new Promise((resolve,reject) =>{ let len = arr.length for(var i = 0;i < len; i++){ let promise = arr[i] if(typeof promise.then == 'function'){ promise.then(res=>{ resolve(res) }) } } }) } Promise.resolve = function (value) { if (value instanceof Promise) return value return new Promise(resolve => resolve(value)) } Promise.reject = function (value) { return new Promise((resolve,reject)=>reject(value)) } module.exports = Promise;