Js实现Promise(符合Promise/A+规范)

// 手写promise
      function Promise(executor) {
        var _this = this
        this.state = 'pending'
        this.value = undefined
        this.reason = undefined
        this.onFullfiledFun = [] // 存储异步
        this.onRejectedFun = [] // 存储异步

        executor(resolve, reject) // 立即执行

        function resolve(value) {
          if (_this.state === 'pending') {
            _this.value = value
            _this.onFullfiledFun.forEach((fn) => fn(value))
            _this.state = 'fulfilled'
          }
        }

        function reject(reason) {
          if (_this.state === 'pending') {
            _this.reason = reason
            _this.onRejectedFun.forEach((fn) => fn(reason))
            _this.state = 'rejected'
          }
        }
      }

      Promise.prototype.then = function (onFullfiled, onRejected) {
        var promise2 = new Promise((resolve, reject) => {
          // pending为等待态,异步还未完成
          if (this.state === 'pending') {
            if (typeof onFullfiled === 'function') {
              this.onFullfiledFun.push(onFullfiled)
            }
            if (typeof onRejected === 'function') {
              this.onRejectedFun.push(onRejected)
            }
          }
          if (this.state === 'fulfilled') {
            if (typeof onFullfiled === 'function') {
              onFullfiled(this.value)
            }
          }
          if (this.state === 'rejected') {
            if (typeof onRejected === 'function') {
              onRejected(this.reason)
            }
          }
        })
        return promise2
      }

      /**
       * 解析then返回值与新Promise对象
       * @param {Object} promise2 新的Promise对象
       * @param {*} x 上一个then的返回值
       * @param {Function} resolve promise2的resolve
       * @param {Function} reject promise2的reject
       */
      function resolvePromise(promise2, x, resolve, reject) {
        if (promise2 === x) {
          return new TypeError('Promise发生了循环引用')
        }
        if (x !== null && (typeof x === 'object' || typeof x === 'function')) {
          // 可能是对象或者函数
          try {
            let then = x.then // 取出then方法引用

            // then是function,那么就执行promise
            if (typeof then === 'function') {
              let y = then.call(
                x,
                (y) => {
                  // 递归调用,传入y若是Promise对象,继续循环
                  resolvePromise(promise2, y, resolve, reject)
                },
                (r) => {
                  reject(r)
                },
              )
            } else {
              resolve(x)
            }
          } catch (e) {
            reject(e)
          }
        } else {
          // 否则是个普通值
          resolve(x)
        }
      }

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端卡卡西呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值