Pormise

 
//Pormise
function MyPromise (executor) {
  var self = this
  self.status = 'pending'
  self.resolveValue = null
  self.rejectValue = null
  self.resolveCallbackList = []  //用来存储resolve的回调函数
  self.rejectCallbackList = []  //用来存储reject的回调函数

  function resolve (value) {
    if (self.status === 'pending') {
      self.status = 'Fulfilled'
      self.resolveValue = value
      self.resolveCallbackList.forEach(function (item) {
        item()
      })
    }
  }

  function reject (value) {
    if(self.status === 'pending') {
      self.status = 'Rejected'
      self.rejectValue = value
      self.rejectCallbackList.forEach(function (item) {
        item()
      })
    }
  }
  try {
    executor(resolve, reject)
  } catch (e) {
    reject(e)
  }
}
function resultIsPromise (nextValue, res, rej) {
    if(nextValue instanceof MyPromise) {
        nextValue.then(function (val) {
            res(val)
        }, function(reason) {
            rej(reason)
        })
    } else {
        res(nextValue)
    }
}
MyPromise.prototype.then = function (resolve, reject) {
  if (!resolve) {
        resolve = function(val) {
          return val
      }
  }
  if (!reject) {
        reject = function (val) {
          throw new Error(val)
      }
  }
  var self = this
  var nextPromise = new MyPromise(function(res, rej) {
    setTimeout(function() {
        if (self.status === 'Fulfilled') {
        try{
            // var newResolveVal = resolve(self.resolveValue)
            // res(newResolveVal)
            var newResolveVal = resolve(self.resolveValue)
            resultIsPromise(newResolveVal, res, rej)
        } catch(e) {
            rej(e)
            }
        }
    }, 0)

    setTimeout(function () {
        if (self.status === 'Rejected') {
        try{
            var newRejectVal = reject(self.rejectValue)
            resultIsPromise(newRejectVal, res, rej)
        } catch(e) {
            rej(e)
        }
        }
    }, 0)

    if (self.status === 'pending') {
      if(typeof resolve === 'function') {
          self.resolveCallbackList.push(function () {
              setTimeout(function() {
                  try{
                      var newResolveVal = resolve(self.resolveValue)
                      resultIsPromise(newResolveVal, res, rej)
                  } catch (e) {
                      rej(e)
                  }
              })
          })
      }
      if(typeof reject === 'function') {
          setTimeout(function () {
              self.rejectCallbackList.push(function () {
                  try{
                      var newRejectVal = reject(self.rejectValue)
                      resultIsPromise(newRejectVal, res, rej)
                  } catch(e) {
                    rej(e)
                  }
              })
          }, 0)
      }
    }
  })
  return nextPromise
}
//race
MyPromise.race = function (promises) {
  return new MyPromise(function (res, rej) {
      promises.forEach( function (item) {
          item.then(res, rej)
      })
  })
}
MyPromise.resolve = function (val) {
  return new MyPromise(function (resolve) {
      resolve(val)
  })
}
MyPromise.catch = function (val) {
  return new MyPromise(function (undefined,reject) {
      reject(val)
  })
}
MyPromise.all = function (promises) {
  return new MyPromise(function(resolve, reject){
    var countNum=0;
    var promiseNum=promises.length;
    var resolvedValue=[];
    for(var i=0;i<promiseNum;i++){
        (function(i){
            promises[i].then(function(value){
                countNum++;
                resolvedValue[i]=value;
                if(countNum===promiseNum){
                    return resolve(resolvedValue)
                }
            },function(reason){
                return reject(reason)
            })
        })(i)
    }
  })
}

  


 

 

 

posted @ 2019-04-29 18:41  那个村  阅读(994)  评论(0编辑  收藏  举报