一个假的Promise

版权声明:任先阳 任 先 阳 任先 先阳,ifgm.cn、www.ifgm.cn、nvcc.cc、www.nvcc.cc、sbfox.com、www.sbfox.com https://blog.csdn.net/qq_39571197/article/details/85067723

中午下班写写,主要没有遵循Promises/A+规范,越写越奇葩,而且还是半成品...

在then阶段只要返回一个函数,就可以获得resolve/reject的控制权,其中还插入了一个setTimeout,用来保证是异步操作

/**
 * 其实还是回调模式,只不过何时触发回调由外部控制(resolve,reject)
 * */
!function (global, namespace) {
  /* util */
  function isFn(fn) {
    return fn instanceof Function;
  }

  const Promise = global[namespace] = class{
    constructor(main) {
      this.RUN_STATUS = 'PENDING'; // PENDING RESOLVED REJECTED
      this.value = null;
      this.resolveCallback = null;
      this.rejectCallback = null;
      if (isFn(main)) {
        main(
          this.getStatusHandler('RESOLVED'),
          this.getStatusHandler('REJECTED')
        );
      }
    }

    getStatusHandler(status) {
      const $this = this;

      function init(args) {
        if ($this.RUN_STATUS !== 'PENDING') return false;
        $this.RUN_STATUS = status;
        $this.value = args;
        return true;
      }

      const operatorMap = {
        RESOLVED() {
          if (!init(arguments)) return;
          setTimeout(() => {
            if (isFn($this.resolveCallback)) {
              $this.resolveCallback();
              $this.resolveCallback = null;
            }
          });
        },
        REJECTED() {
          if (!init(arguments)) return;
          setTimeout(() => {
            if (isFn($this.rejectCallback)) {
              $this.rejectCallback();
              $this.rejectCallback = null;
            }
          });
        },
      };
      return operatorMap[status] || function () {
        throw('不好意思,内部错误');
      };
    }


    then(resolveCb, rejectCb) {
      if (!isFn(resolveCb) || !isFn(rejectCb)) return;
      return new Promise((resolve, reject) => {
        this.resolveCallback = () => {
          const resolveCbResult = resolveCb(...this.value);
          if (isFn(resolveCbResult)) {
            resolveCbResult(resolve, reject);
          } else {
            resolve(resolveCbResult);
          }
        };
        this.rejectCallback = () => {
          const rejectCbResult = rejectCb(...this.value);
          if (isFn(rejectCbResult)) {
            rejectCbResult(resolve, reject);
          } else {
            reject(rejectCbResult);
          }
        };
      });
    }

    static resolve(main) {
      return new Promise((resolve, reject) => {
        if (isFn(main)) {
          main(resolve, reject);
        } else {
          resolve(main);
        }
      });
    }

    static reject(main) {
      return new Promise((resolve, reject) => {
        if (isFn(main)) {
          main(resolve, reject);
        } else {
          reject(main);
        }
      });
    }

    static all(promisesList) {
      const resultsList = [];
      //转map
      const promisesMap = {};
      //..上班了
    }
  }
}(global, 'Q');
new Q((resolve, reject) => {
  resolve(1);
}).then(data => {
  console.log(data, 1);
  return function (resolve, reject) {
    reject(1);
    resolve(1);
  };
}, err => {
  console.log(err, 2);
}).then(data => {
  console.log(data, 3);
}, err => {
  console.log(err, 4);
});
Q.resolve().then(data => {
  console.log(1111);
}, err => {
  console.log(2222);
});
Q.reject().then(data => {
  console.log(111);
}, err => {
  console.log(222);
})

 

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试