您访问的是非正式的站点,不是最新内容哦,请点击这里前往Dnocm ヾ(=゚・゚=)ノ喵♪

若白驹过隙,忽然而已

造了套自己的 Promise

Promise 很火,没看源码,仅参考平时的使用,造了一个小轮子(有时间再去看源码把)

class PromiseT {
  constructor() {
    this.status = 'running';
    this.val = null;
    this.pendingList = [];
  }
  then(...list) {
    this.pendingList.push(...list);
    return this.run();
  }
  resolve(val) {
    this.status = 'running';
    if (val !== undefined) {
      this.val = val;
    }
    return this.run();
  }
  pending() {
    this.status = 'pending';
  }
  run() {
    if (this.status === 'pending') {
      return this;
    }
    const cal = this.pendingList.shift();
    if (cal === undefined) {
      return this;
    }
    const result = cal(this.val, this);
    if (result === undefined) {
      return this.run();
    }
    if (result.constructor === PromiseT) {
      return result.then(...this.pendingList);
    }
    this.val = result;
    return this.run();
  }
}

测试:

new PromiseT()
  .resolve(5)
  .then(x => x + 1)
  .then(x => new PromiseT().resolve(2).then(a => x / a))
  .then(x => console.log(x))
  .then((x, p) => {
    p.pending();
    setTimeout(() => {
      p.resolve(x - 3);
    }, 2000);
  })
  .then(x => console.log(x));

// 输出 3
// 等待 2s
// 输出 0

毕竟是小轮子,catch() finally() 这些没写。。。