js类似promise的手写实现

function MyPromise(func){
            this.status = "pending";
            this.rightResult = null;
            this.errorResult = null;
            this.errorFunction = true;
            this.out = false;
            let self = this
            function resolve(value){
                if(self.out) return 
                self.status = 'resolved'
                self.rightResult = value
                self.out = true
            };
            function reject(value){
                if(self.out) return 
                self.status = 'rejected'
                self.errorResult = value
                self.out = true
            };
            try{
                func(resolve,reject)
            }catch(e){
                console.log(e)
            }
        }
    MyPromise.prototype.then = function(onResolved,onRejected){
          if (typeof onRejected !== 'function'){
              this.errorFunction = false
          }
          if(this.status === 'resolved' && typeof onResolved === 'function'){
              onResolved(this.rightResult)
          }
          else if(this.status === 'rejected' && typeof onResolved === 'function'){
              onRejected(this.errorResult)
          }
      }
      MyPromise.prototype.catch = function(onRejected){
          if(this.status = 'rejected' && !this.errorFunction){
              onRejected(this.errorResult)
          }
      }
上一篇:五子棋AI算法人机对战(春物彩羽版)


下一篇:队列的基本操作的实现