手写es5版的promise

  function resolvePromise(promise2,x,resole,reject) {
    if (promise2 === x) {
      try {
        //return reject(new Error("循环引用"));
      }catch (e) {
        return reject("循环引用");
      }
    }
    if (typeof x === "function" || (typeof x === "object" && x !== null)) {
      var called;
      try {
        let then = x.then;
        if (typeof then === "function") {
          then.call(function (x,y) {
            if (called) return;
            called = true;
            resolvePromise(promise2, y, resole, reject);
          },function (r) {
            if (called) return;
            called = true;
            reject(r);
          })
        }else {
          resole(x);
        }
      }catch (e) {
        if (called) return;
        called = true;
        reject(e);
      }
    }else {
      resole(x);
    }
  }
  function Promise(executor) {
    this.value = "";
    this.reason = "";
    this.status = "pending";

    this.resolveCallbacks = [];
    this.rejectedCallbacks = [];
    try {
      executor(this.resolve.bind(this),this.reject.bind(this))
    }catch (e) {
      this.reject.call(this,e);
    }
  }
  Promise.prototype.resolve = function (value) {
    if (this.status === "pending") {
      this.status = "fulfilled";
      this.value = value;
      for (var i = 0; i < this.resolveCallbacks.length; i++) {
        this.resolveCallbacks[i]();
      }
    }
  };
  Promise.prototype.reject = function (reason) {
    if (this.status === 'pending') {
      this.status = "rejected";
      this.reason = reason;
      for (var i = 0; i < this.rejectedCallbacks.length; i++) {
        this.rejectedCallbacks[i]();
      }
    }
  };
  Promise.prototype.then = function (onfulfilled,onrejected) {
    var promise2,_this=this;

    promise2 = new Promise(function (resolve, reject) {
      if (_this.status === "fulfilled") {
        setTimeout(function () {
          try {
            var x = onfulfilled(_this.value);
            resolvePromise(promise2, x, resolve, reject);
          } catch (e) {
            reject(e);
          }
        }, 0);
      }

      if (_this.status === "rejected") {
        setTimeout(function () {
          try {
            let x = onrejected(_this.reason);
            resolvePromise(promise2, x, resolve, reject);
          } catch (e) {
            reject(e);
          }
        }, 0);
      }

      if (_this.status === "pending") {
        _this.resolveCallbacks.push(function () {
          setTimeout(function () {
            try {
              let x = onfulfilled(_this.value);
              resolvePromise(promise2, x, resolve, reject);
            } catch (e) {
              reject(e);
            }
          },0)
        });

        _this.rejectedCallbacks.push(function () {
          setTimeout(function () {
            try {
              let x = onrejected(_this.reason);
              resolvePromise(promise2, x, resolve, reject);
            } catch (e) {
              reject(e);
            }
          }, 0);
        })


      }
    });
    return promise2;
  };

  Promise.prototype.catch = function (rejectFuc) {
    return this.then(null, rejectFuc);
  };
  Promise.prototype.finally = function (callback) {
    this.then(callback, callback);
  };
  Promise.resolve = function (value) {
    return new Promise(function (resolve, reject) {
      resolve(value);
    });
  };
  Promise.reject = function (reason) {
    return new Promise(function (resolve,reject) {
      reject(reason);
    })
  };
  Promise.race = function (values) {
    return new Promise(function (resolve,reject) {
      for (var i=0;i<values.length;i++){
        var current = values[i];
        if (typeof current === "function" || (typeof current === "object") && current !== null) {
          var then = current.then;
          if (typeof then === "function") {
            then.call(current, resolve, reject);
          }else {
            reject(current);
          }
        }else {
          resolve(current);
        }
      }
    })
  };

  var P = new Promise(function (resolve,reject) {
    resolve(11);
  });
  P.then(function (data) {

  });


发布了80 篇原创文章 · 获赞 5 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_28473733/article/details/105457443