手写Promise(依次设计,方便理解)

https://www.bilibili.com/video/BV1Lb411f766?p=14&vd_source=0910f4debbd07e1a7671bae76639d8fe

// 1. 手写Promise - 结构的设计

const PROMISE_STATUS_PENDING = 'pending';
const PROMISE_STATUS_FULFILLED = 'fulfilled';
const PROMISE_STATUS_REJECTED = 'rejected';

class MyPromise {
    constructor(executor) {
        this.status = PROMISE_STATUS_PENDING;

        const resolve = value => {
            if (this.status === PROMISE_STATUS_PENDING) {
                this.status = PROMISE_STATUS_FULFILLED;

                console.log('resolve');
            }
        }

        const reject = reason => {
            if (this.status === PROMISE_STATUS_PENDING) {
                this.status = PROMISE_STATUS_REJECTED;

                console.log('reject');
            }
        }

        executor(resolve, reject);
    }
}




const p = new MyPromise((resolve, reject) => {
    resolve();
    reject();
})

// 2. 手写Promise - then方法设计

const PROMISE_STATUS_PENDING = 'pending';
const PROMISE_STATUS_FULFILLED = 'fulfilled';
const PROMISE_STATUS_REJECTED = 'rejected';

class MyPromise {
    constructor(executor) {
        this.status = PROMISE_STATUS_PENDING;
        this.value = undefined;
        this.reason = undefined;

        const resolve = value => {
            if (this.status === PROMISE_STATUS_PENDING) {
                this.status = PROMISE_STATUS_FULFILLED;

                queueMicrotask(() => {
                    this.value = value;
                    this.onFulfilled(this.value)
                })

                console.log('resolve');
            }
        }

        const reject = reason => {
            if (this.status === PROMISE_STATUS_PENDING) {
                this.status = PROMISE_STATUS_REJECTED;

                queueMicrotask(() => {
                    this.reason = reason;
                    this.onRejected(this.reason)
                })
                console.log('reject');
            }
        }

        executor(resolve, reject);
    }

    then(onFulfilled, onRejected) {
        this.onFulfilled = onFulfilled;
        this.onRejected = onRejected;
    }
}




const p = new MyPromise((resolve, reject) => {
    resolve(111);
    reject(222);
})

p.then(res => {
    console.log(res, 'res1');
}, err => {
    console.log(err, 'err1');
})

// 3. 手写Promise - then方法优化(支持调用多次then,定时器内使用then方法)

  const PROMISE_STATUS_PENDING = 'pending';
  const PROMISE_STATUS_FULFILLED = 'fulfilled';
  const PROMISE_STATUS_REJECTED = 'rejected';

  class MyPromise {
    constructor(executor) {
      this.status = PROMISE_STATUS_PENDING;
      this.value = undefined;
      this.reason = undefined;
      this.onFulfilledFns = [];
      this.onRejectedFns = [];


      const resolve = (value) => {
        if (this.status === PROMISE_STATUS_PENDING) {
          queueMicrotask(() => {
            if (this.status !== PROMISE_STATUS_PENDING) return; // 阻止成功和失败一起调用
            this.status = PROMISE_STATUS_FULFILLED;
            this.value = value;
            this.onFulfilledFns.forEach(fn => fn(this.value))
          })
        }
      }

      const reject = (reason) => {
        if (this.status === PROMISE_STATUS_PENDING) {
          queueMicrotask(() => {
            if (this.status !== PROMISE_STATUS_PENDING) return; // 阻止成功和失败一起调用
            this.status = PROMISE_STATUS_REJECTED;
            this.reason = reason;
            this.onRejectedFns.forEach(fn => fn(this.reason))
          })
        }
      }

      executor(resolve, reject)
    }

    then(onFulfilled, onRejected) {
      if (this.status === PROMISE_STATUS_FULFILLED && onFulfilled) {
        onFulfilled(this.value)
      } else if (this.status === PROMISE_STATUS_REJECTED && onRejected) {
        onRejected(this.reason)
      } else if (this.status === PROMISE_STATUS_PENDING) {
        this.onFulfilledFns.push(onFulfilled)
        this.onRejectedFns.push(onRejected)
      }
    }
  }

  const p = new MyPromise((resolve, reject) => {
    resolve(111);
    reject(222);
  })

  p.then((res) => {
    console.log(res, 'res1');
  }, (err) => {
    console.log(err, 'err1');
  })

  p.then((res) => {
    console.log(res, 'res2');
  }, (err) => {
    console.log(err, 'err2');
  })

  setTimeout(() => {
    p.then((res) => {
      console.log(res, 'res3');
    }, (err) => {
      console.log(err, 'err3');
    })
  }, 1000);


  // 4. 手写Promise - then链式调用

  const PROMISE_STATUS_PENDING = 'pending';
  const PROMISE_STATUS_FULFILLED = 'fulfilled';
  const PROMISE_STATUS_REJECTED = 'rejected';

  class MyPromise {
    constructor(executor) {
      this.status = PROMISE_STATUS_PENDING;
      this.value = undefined;
      this.reason = undefined;
      this.onFulfilledFns = [];
      this.onRejectedFns = [];


      const resolve = (value) => {
        if (this.status === PROMISE_STATUS_PENDING) {
          queueMicrotask(() => {
            if (this.status !== PROMISE_STATUS_PENDING) return;
            this.status = PROMISE_STATUS_FULFILLED;
            this.value = value;
            this.onFulfilledFns.forEach(fn => fn(this.value))
          })
        }
      }

      const reject = (reason) => {
        if (this.status === PROMISE_STATUS_PENDING) {
          queueMicr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值