手写promise

Promise.prototype.myRace = function (target) {
  let arr = Array.from(target);
  return new Promise((resolve, reject) => {
    for(let i=0; i<arr.length; i++){
      if(typeof arr[i].then === 'function'){
        arr[i].then(value => resolve(value), reject);
      }else{
        resolve(arr[i]);
      }
    }
  })
}

Promise.prototype.myAll = function(target){
  let arr = Array.from(target);
  return new Promise((resolve, reject) => {
    let result = [], count = 0;
    const length = arr.length;
    function getResult(val, index) {
      result[index] = val;
      count++;
      if(count === length){
        resolve(result);
      }
    }

    for(let i=0; i<length; i++){
      if(typeof arr[i].then === 'function'){
        arr[i].then(val => getResult(val, i), reject);
      }else{
        getResult(arr[i], i);
      }
    }
  })
}

Promise.prototype.myCatch = function(callBack){
  return this.then(null, callBack);
}

Promise.prototype.myFinally = function(callBack){
  return this.then(value => {
    return Promise.resolve(callBack()).then(() => value);
  }, err => {
    return Promise.resolve(callBack()).then(() => err);
  })
}

let p1 = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('123');
  }, 1000)
})
let p2 = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('123');
  }, 4000)
})

const PENDING = 'pending';
const RESOLVED = 'resolved';
const REJECTED = 'rejected';
class MyPromise{
  constructor(exec) {
    this.status = PENDING;
    this.value = undefined;
    this.reason = undefined;
    this.resolvedCb = [];
    this.rejectedCb = [];

    let resolve = value => {
      this.value = value;
      this.status = RESOLVED;
      this.resolvedCb.map(fn => fn());
    }

    let reject = reason => {
      this.reason = reason;
      this.status = REJECTED;
      this.rejectedCb.map(fn => fn());
    }

    try {
      exec(resolve, reject);
    }catch (e) {
      console.log(e, 'err');
    }
  }

  then(onResolve, onReject){
    return new MyPromise((resolve, reject) => {
      if(this.status === RESOLVED){
        let value = onResolve(this.value);
        resolve(value);
      }else if(this.status === REJECTED){
        let reason = onReject(this.reason);
        reject(reason);
      }else{
        this.resolvedCb.push(() => {
          let value = onResolve(this.value);
          resolve(value);
        })

        this.rejectedCb.push(() => {
          let reason = onReject(this.reason);
          reject(reason);
        })
      }
    })
  }
}

Promise.prototype.myRace([p1, p2]).then(() => console.log('test'))
Promise.prototype.myAll([p1, p2]).then(() => console.log('test'))

new MyPromise((resolve, reject) => {
  setTimeout(() => {
    resolve('p1');
  }, 1000)
}).then(val => {
  console.log(val);
  return 'p2'
}).then(val => {
  console.log('p2');
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值