手写promise

//promise三种状态(运行中,执行成功,执行失败)


const PENDING='pending'
const RESOLVE='fulfilled'
const REJECTED='rejected'


function MyPromise(executor){
    this.data=undefined
    this.Fullcallback=[]//存放成功的回调
    this.Failedcallback=[]//存放失败的回调
    this.status=PENDING

    function resolve(data){
        if(this.status===PENDING){
            this.status=RESOLVE
            this.data=data;
            this.Fullcallback.forEach(element => {
                setTimeout(()=>{element(this.data)},0)
            });
        }
    }
    function reject(err){
        if(this.status===PENDING){
            this.status=REJECTED
            this.data=err
            this.Failedcallback.forEach(element=>{
                setTimeout(()=>{element(this.data)},0)
            })
        }
    }
    try{executor(resolve,reject)}catch(e){
        reject(e)
    }
}

MyPromise.prototype.then=function(onFulfilled,onRejected){
    onFulfilled=typeof onFulfilled==='function'?onFulfilled:val=>val
    onRejected=typeof onRejected==='function'?onRejected:err=>{throw err};
    var self=this;
    if(self.status===PENDING){
      return promise2=new MyPromise((resolve,reject)=>{
          self.Fullcallback.push((value)=>{
              try{
                 let x=onFulfilled(value)
                 resolvePromise(promise2,x,resolve,reject)
              }catch(e){
                    reject(e)
              }
          })
          self.Failedcallback.push((value)=>{
              try{
                  let x=onRejected(value)
                  resolvePromise(promise2,x,resolve,reject)
              }catch(e){
                  reject(e)
              }
          })
      })
    }
    if(self.status===RESOLVE){
        return promise2=new MyPromise((resolve,reject)=>{
            setTimeout(()=>{
                try{
                    let x=onFulfilled(self.data)
                    resolvePromise(promise2,x,resolve,reject)
                }catch(e){
                    reject(e)
                }
            },0)
        })
    }
    if(self.status===REJECTED){
        return promise2=new MyPromise((resolve,reject)=>{
            setTimeout(()=>{
                try{
                    let x=onRejected(self.data)
                    resolvePromise(promise2,x,resolve,reject)
                }catch(e){
                    reject(e)
                }
            })
        })
    }
}

function resolvePromise(promise2,x,resolve,reject){
    if(promise2===x){throw Error()}
    let called=false;
    if(x!==null&&typeof x=='function'||typeof x=='object'){
    try{
        let then=x.then
        if(typeof then==='function'){
            try{
            then.call(x,y=>{
                if(called)return
                called=true
                resolvePromise(promise2,y,resolve,reject)
            })}catch(e){
                if(called)return;
                called=true;
                reject(e)
            }
        }else{
            resolve(x)
        }
    }catch(e){
        if(called)return 
        called=true;
        reject(e)
    }
    }else{
        resolve(x)
    }
}

MyPromise.resolve=function(data){
    return new MyPromise(reslove=>{reslove(data)})
}
MyPromise.reject=function(data){
    return new MyPromise((resolve,reject)=>{reject(data)})
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值