손으로 쓰는es5판의promise

3922 단어 js
  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

좋은 웹페이지 즐겨찾기