You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
then 的链式调用,需要返回Promise,所以new的promise2要判断onFulfilled返回的是否是Promise
代码
classmyPromise{constructor(executor){// 0. Check executorif(typeofexecutor!='function'){thrownewTypeError('executor is not a fn')}// 1. initthis.initStatus();this.initValue();this.initBind();// 2. execute executortry{executor(this.resolve,this.reject)}catch(error){this.reject(error)}}initStatus(){this.STATUS_RESOLVED=Symbol('resolved');this.STATUS_REJECTED=Symbol('rejectd');this.STATUS_PENDING=Symbol('pending');}initValue(){this.value=null;this.reason=null;this.status=this.STATUS_PENDING;this.onFulfilledCallbacks=[];this.onRejectedCallbacks=[];}initBind(){this.resolve=this.resolve.bind(this)this.reject=this.reject.bind(this)}resolve(value){setTimeout(()=>{console.log('resolve');console.log(this.status);if(this.status==this.STATUS_PENDING){// change status and valuethis.status=this.STATUS_RESOLVED;this.value=value;this.onFulfilledCallbacks.forEach(el=>el(this.value))}})}reject(reason){setTimeout(()=>{if(this.status==this.STATUS_PENDING){// change status and valuethis.status=this.STATUS_REJECTED;this.reason=reason;this.onRejectedCallbacks.forEach(el=>el(this.reson))}})}// chain callbackthen(onFulfilled,onRejected){// 0. Check the argumentsif(typeofonFulfilled!='function'){onFulfilled=(value)=>{returnvalue;}}if(typeofonRejected!='function'){onRejected=(reson)=>{returnreson;}}// 1. onFulfilled if(this.status===this.STATUS_RESOLVED){console.log('onFulfilled')constmyPromise2=newmyPromise((resolve,reject)=>{try{letx=onFulfilled(this.value)if(xinstanceofmyPromise){x.then(resolve,reject)// pass the result to myPromise2}else{console.log("this.resolve(x);")resolve(x);}}catch(error){this.reject(error)}})returnmyPromise2;}// 2. onRejectedif(this.status===this.STATUS_REJECTED){console.log('onRejected')constmyPromise2=newmyPromise((resolve,reject)=>{try{letx=onRejected(this.reason)if(xinstanceofmyPromise){x.then(resolve,reject)// pass the result to myPromise2}}catch(error){reject(error)}})returnmyPromise2;}// 3. onPendingif(this.status===this.STATUS_PENDING){console.log('onPending')constmyPromise2=newmyPromise((resolve,reject)=>{// 3.1 For onFulfilled: push a fn with setTimeoutthis.onFulfilledCallbacks.push((value)=>{try{letx=onFulfilled(value)if(xinstanceofmyPromise){x.then(resolve,reject)// pass the result to myPromise2}else{console.log("this.resolve(x);")resolve(x);}}catch(error){this.reject(error)}})// 3.2 For onRejected: push a fn with setTimeoutthis.onRejectedCallbacks.push((reason)=>{try{letx=onRejected(reason)if(xinstanceofmyPromise){x.then(resolve,reject)// pass the result to myPromise2}}catch(error){reject(error)}})})returnmyPromise2}}}myPromise.prototype.catch=(onRejected)=>{returnthis.then(null,onRejected)}module.exports=myPromise
The text was updated successfully, but these errors were encountered:
Promise的实现
这个略复杂,我大概用了100多行代码实现了链式调用,先上代码
关键点
this.onFulfilledCallbacks = []
代码
The text was updated successfully, but these errors were encountered: