constPENDING='pending'constFULFILLED='fulfilled'constREJECTED='rejected'classMPromise{FULFILLED_CALLBACK_LIST=[]REJECTED_CALLBACK_LIST=[]
_status =PENDINGconstructor(fn){// 初始状态为pendingthis.status =PENDINGthis.value =nullthis.reason =nulltry{fn(this.resolve.bind(this),this.reject.bind(this))}catch(e){this.reject(e)}}getstatus(){returnthis._status
}setstatus(newStatus){this._status = newStatus
switch(newStatus){caseFULFILLED:{this.FULFILLED_CALLBACK_LIST.forEach((callback)=>{callback(this.value)})break}caseREJECTED:{this.REJECTED_CALLBACK_LIST.forEach((callback)=>{callback(this.reason)})break}}}resolve(value){if(this.status ===PENDING){this.value = value
this.status =FULFILLED}}reject(reason){if(this.status ===PENDING){this.reason = reason
this.status =REJECTED}}then(onFulfilled, onRejected){const realOnFulfilled =this.isFunction(onFulfilled)?onFulfilled:(value)=>{return value
}const realOnRejected =this.isFunction(onRejected)?onRejected:(reason)=>{throw reason
}const promise2 =newMPromise((resolve, reject)=>{constfulfilledMicrotask=()=>{queueMicrotask(()=>{try{const x =realOnFulfilled(this.value)this.resolvePromise(promise2, x, resolve, reject)}catch(e){reject(e)}})}constrejectedMicrotask=()=>{queueMicrotask(()=>{try{const x =realOnRejected(this.reason)this.resolvePromise(promise2, x, resolve, reject)}catch(e){reject(e)}})}switch(this.status){caseFULFILLED:{fulfilledMicrotask()break}caseREJECTED:{rejectedMicrotask()break}casePENDING:{this.FULFILLED_CALLBACK_LIST.push(fulfilledMicrotask)this.REJECTED_CALLBACK_LIST.push(rejectedMicrotask)}}})return promise2
}catch(onRejected){returnthis.then(null, onRejected)}isFunction(param){returntypeof param ==='function'}resolvePromise(promise2, x, resolve, reject){// 如果 newPromise 和 x 指向同一对象,以 TypeError 为据因拒绝执行 newPromise// 这是为了防止死循环if(promise2 === x){returnreject(newTypeError('The promise and the return value are the same'))}if(x instanceofMPromise){// 如果 x 为 Promise ,则使 newPromise 接受 x 的状态// 也就是继续执行x,如果执行的时候拿到一个y,还要继续解析yqueueMicrotask(()=>{
x.then((y)=>{this.resolvePromise(promise2, y, resolve, reject)}, reject)})}elseif(typeof x ==='object'||this.isFunction(x)){// 如果 x 为对象或者函数if(x ===null){// null也会被判断为对象returnresolve(x)}let then =nulltry{// 把 x.then 赋值给 then
then = x.then
}catch(error){// 如果取 x.then 的值时抛出错误 e ,则以 e 为据因拒绝 promisereturnreject(error)}// 如果 then 是函数if(this.isFunction(then)){let called =false// 将 x 作为函数的作用域 this 调用// 传递两个回调函数作为参数,第一个参数叫做 resolvePromise ,第二个参数叫做 rejectPromisetry{then.call(
x,// 如果 resolvePromise 以值 y 为参数被调用,则运行 resolvePromise(y)=>{// 需要有一个变量called来保证只调用一次.if(called)return
called =truethis.resolvePromise(promise2, y, resolve, reject)},// 如果 rejectPromise 以据因 r 为参数被调用,则以据因 r 拒绝 promise(r)=>{if(called)return
called =truereject(r)})}catch(error){// 如果调用 then 方法抛出了异常 e:if(called)return// 否则以 e 为据因拒绝 promisereject(error)}}else{// 如果 then 不是函数,以 x 为参数执行 promiseresolve(x)}}else{// 如果 x 不为对象或者函数,以 x 为参数执行 promiseresolve(x)}}staticresolve(value){if(value instanceofMPromise){return value
}returnnewMPromise((resolve)=>{resolve(value)})}staticreject(reason){returnnewMPromise((resolve, reject)=>{reject(reason)})}staticrace(promiseList){returnnewMPromise((resolve, reject)=>{const length = promiseList.length
if(length ===0){returnresolve()}else{for(let i =0; i < length; i++){
MPromise.resolve(promiseList[i]).then((value)=>{returnresolve(value)},(reason)=>{returnreject(reason)})}}})}}
const test =newMPromise((resolve, reject)=>{setTimeout(()=>{reject(111)},1000)}).then((value)=>{
console.log('then')}).catch((reason)=>{
console.log('catch')})
MPromise.resolve(1).then((res)=>{
console.log(res,'res')return2}).then((res2)=>{
console.log(res2,'res2')})