//三种状态constPENDING="pending";constRESOLVED="已解决";constREJECTED="拒绝";//promise接收一个函数参数,将立即执行functionMyPromise(fn){let_this=this;_this.currentState=待定;_this.value=未定义;//用于then中保存回调,只有当promise状态为pending时,每个实例最多缓存一个_this.resolvedCallbacks=[];_this.rejectedCallbacks=[];_this.resolve=function(value){if(valueinstanceofMyPromise){//如果value是Promise,递归执行returnvalue.then(_this.resolve,_this.reject);}setTimeout(()=>{//异步执行,必须保证执行顺序if(_this.currentState===PENDING){_this.currentState=RESOLVED;_this.value=value;_this.resolvedCallbacks.for每个((cb)=>cb());}});};_this.reject=function(reason){setTimeout(()=>{//异步执行,保证执行顺序if(_this.currentState===PENDING){_this.currentState=REJECTED;_this.value=reason;_this.rejectedCallbacks.forEach((cb)=>cb());}});};//用于解决下面的问题//newPromise(()=>throwError('error))try{fn(_this.resolve,_this.reject);}catch(e){_this.reject(e);}}MyPromise.prototype.then=function(onResolved,onRejected){varself=this;//Specification2.2.7,thenmustreturnanewpromisevarpromise2;//规范2.2。onResolved和onRejected都是可选参数//如果类型不是函数,需要忽略,透传//Promise.resolve(v).then().then((value)=>console.log(value))onResolved=typeofonResolved==="function"?onResolved:(v)=>v;onRejected=typeofonRejected===“功能”?onRejected:(r)=>{抛出r};if(self.currentState===RESOLVED){return(promise2=newMyPromise(function(resolve,reject){//规范2.2.4,保证onFulfilled,onRjected异步执行//所以用了setTimeout封装下setTimeout(function(){try{varx=onResolved(self.value);resolutionProcedure(promise2,x,resolve,reject);}catch(reason){reject(reason);}});}));}if(self.currentState===REJECTED){return(promise2=newMyPromise(function(resolve,reject){setTimeout(function(){//异步执行onRejectedtry{varx=onRejected(self.value);resolutionProcedure(promise2,x,resolve,reject);}catch(reason){reject(原因);}});}));}if(self.currentState===PENDING){return(promise2=newMyPromise(function(resolve,reject){self.resolvedCallbacks.push(function(){//考虑到可能会出错,使用try/catch来换行尝试{varx=onResolved(self.value);resolutionProcedure(promise2,x,resolve,reject);}catch(r){reject(r);}});自己。拒绝回调。push(function(){try{varx=onRejected(self.value);resolutionProcedure(promise2,x,resolve,reject);}catch(r){reject(r);}});}));}};//规范2.3functionresolutionProcedure(promise2,x,resolve,reject){//规范2.3.1,x不能和promise2相同,避免循环引用if(promise2===x){returnreject(newTypeError("错误”));}//Specification2.3.2//如果x是一个Promise,状态是pending,需要继续等待,否则执行if(xinstanceofMyPromise){if(x.currentState===PENDING){x.then(function(value){//再次调用此函数,确认xresolve//参数的类型,如果是基本类型,则再次resolve//传值给下一个thenresolutionProcedure(promise2,value,resolve,reject);},拒绝);}else{x.then(解决,拒绝);}返回;}//Specification2.3.3.3.3//如果reject或resolve其中一个已经执行过,则忽略另一个letcalled=false;//规范2.3.3,判断x是对象还是函数if(x!==null&&(typeofx==="object"||typeofx==="function")){//规范2.3.3.2,如果then不能提取,拒绝try{//Specification2.3.3.1letthen=x.then;//如果then是一个函数,调用x.thenif(typeofthen==="function"){//规范2.3.3.3then.call(x,(y)=>{if(called)return;称为=真;//规范2.3.3.3.1resolutionProcedure(promise2,y,resolve,reject);},(e)=>{如果(调用)返回;称为=真;拒绝(e);});}else{//规范2.3.3.4resolve(x);}}catch(e){如果(调用)返回;称为=真;拒绝(e);}}else{//规范2.3.4,x是原始类型resolve(x);}}async/await是基于生成器的语法糖:async函数返回一个promise对象constasync=(func)=>{constp=newPromise((resolve,reject)=>{try{constvalue=func()if(((typeofvalue==='object'&&value!==null)||typeofvalue==='function')&&typeofvalue.then==='function'){Promise.resolve(value).then(resolve,reject)}else{resolve(value)}}catch(error){reject(error)}})returnp}constawait=(arg)=>(onResolved,onRejected)=>{constinnerPromise=onRejected复制代码?Promise.resolve(arg).catch(onRejected).then(onResolved,onRejected):Promise.resolve(arg).then(onResolved,onRejected)返回innerPromise}
