class MyPromise {
  state = 'pedding'; // 状态 pedding fulfilled rejected
  value = undefined; // fulfilled 状态的值
  reson = undefined; // rejected 状态的哦值
  resolveCallbacks = []; // fulfilled 状态存储成功回调
  rejecteCallbacks = []; // rejected 状态存储失败回调
  constructor(fn){
    const resolveHandler = (value) => {
      if (this.state === 'pedding') {
        this.state = 'fulfilled';
        this.value = value;
        this.resolveCallbacks.forEach(v => v(this.value));
      }
    }

    const rejectHandler = (value) => {
      if (this.state === 'pedding') {
        this.state = 'rejected'
        this.reson = value;
        this.rejecteCallbacks.forEach(v =>  v(this.reson));
      }
    }
    try {
      fn(resolveHandler, rejectHandler);
    } catch (error) {
      rejectHandler(error);
    }
  }

  then(fn1, fn2){
    // 当pedding 状态下,fn1 fn2会被缓存
    fn1 = typeof fn1 === 'function' ? fn1 : (v) => v
    fn2 = typeof fn2 === 'function' ? fn2 : (e) => e

    if (this.state === 'pedding') {
      return new MyPromise((resovle, reject) =>{
        this.resolveCallbacks.push(()=> {
          try {
            let newVal = fn1(this.value);
            resovle(newVal);
          } catch (error) {
            reject(error)
          }
        })
        this.rejecteCallbacks.push(()=> {
          try {
            let newVal = fn2(this.reson);
            reject(newVal);
          } catch (error) {
            reject(error)
          }
        })
      })
    }

    if (this.state === 'fulfilled') {
      return new MyPromise((resovle, reject) =>{
        try {
          let newVal = fn1(this.value);
          resovle(newVal);
        } catch (error) {
          reject(error)
        }
      })
    }

    if (this.state === 'rejected') {
      return new MyPromise((resovle, reject) =>{
        try {
          let newVal = fn2(this.reson);
          reject(newVal);
        } catch (error) {
          reject(error)
        }
      })
    }
  }
  catch(fn) {
    return this.then(null, fn)
  }
}

MyPromise.resovle = function(value){
  return new MyPromise((resovle) => resovle(value))
}

MyPromise.reject = function(value){
  return new MyPromise((resovle, reject) => reject(value))
}

// 全部执行成功
MyPromise.all = function(promiseList = []) {
  return new MyPromise((resolve, reject)=> {
    const result = [];
    const length = promiseList.length;
    let resolveCount = 0;
    promiseList.forEach((p, index) => {
      p.then(data=> {
        result.push(data);
        resolveCount++;
        if (resolveCount === length) {
          resolve(result);
        }
      }).catch(err => {
        reject(err)
      })
    })
  });
}
// 一个执行完
MyPromise.race = function(promiseList = []) {
  return new MyPromise((resolve, reject)=> {
    let resovled = false;
    const result = [];
    const length = promiseList.length;
    promiseList.forEach((p, index) => {
      p.then(data => {
        if (!resovled) {
          resolve(data);
          resovled = true;
        }
      }).catch(err => {
        if (!resovled) {
          reject(err)
          resovled = true;
        }
      })
    })
  });
}

 

posted on 2019-04-28 11:23  浅唱年华1920  阅读(230)  评论(0编辑  收藏  举报