Promise源码实现与测试

时间:2022-05-27 23:05:15   收藏:0   阅读:7
 const PENDING = ‘pending‘,
        FULFILLED = ‘fulfilled‘,
        REJECTED = ‘rejected‘

      class MyPromise {
        constructor(executor) {
          this.state = PENDING
          this.value = undefined
          this.reason = undefined
          this.onResolvedCallbacks = []
          this.onRejectedCallbacks = []

          let resolve = (value) => {
            if (this.state === PENDING) {
              this.state = FULFILLED
              this.value = value
              this.onResolvedCallbacks.forEach((fn) => fn())
            }
          }

          let reject = (reason) => {
            if (this.state === PENDING) {
              this.state = REJECTED
              this.reason = reason
              this.onRejectedCallbacks.forEach((fn) => fn())
            }
          }
          try {
            executor(resolve, reject)
          } catch (err) {
            reject(err)
          }
        }

        then(onFulFilled, onRejected) {
          let p2 = new MyPromise((resolve, reject) => {
            let x
            if (this.state === FULFILLED) {
              setTimeout(() => {
                x = onFulFilled(this.value)
                //resolve(x);

                resolvePromise(p2, x, resolve, reject)

                // x 决定 了 p2 的状态, resolve(x)或者 reject(x);
              }, 0)
            }

            if (this.state === REJECTED) {
              x = onRejected(this.reason)
              resolvePromise(p2, x, resolve, reject)
            }

            if (this.state === PENDING) {
              this.onResolvedCallbacks.push(() => {
                x = onFulFilled(this.value)
                resolvePromise(p2, x, resolve, reject)
              })
              this.onRejectedCallbacks.push(() => {
                x = onRejected(this.reason)
                resolvePromise(p2, x, resolve, reject)
              })
            }
          })

          return p2
        }
      }

      function resolvePromise(p2, x, resolve, reject) {
        if (p2 === x) {
          return new Error(‘引用错误‘)
        }
        // thenable 对象;  blueBird q;
        if ((typeof x === ‘object‘ && x !== null) || typeof x === ‘function‘) {
          try {
            let then = x.then
            if (typeof then === ‘function‘) {
              then.call(
                x,
                (y) => {
                  resolvePromise(p2, y, resolve, reject)
                },
                (err) => {
                  reject(err)
                }
              )
            }
          } catch (err) {
            reject(err)
          }
        } else {
          resolve(x)
        }
      }
      function test() {
        return new MyPromise((resolve, reject) => {
          setTimeout(() => {
            resolve(100)
          }, 1000)
        })
      }

      var p1 = test()
      // p1 => resolve(100) =>  p1.then(res)  res => 100;
      // p2  => resolve(res)  =>  p2.then(res) => res ??

      var p2 = p1.then(
        (res) => {
          // return {
          //   then(resolve, reject) {
          //     reject(100000);
          //   }
          // };
          return new MyPromise((resolve, reject) => {
            resolve(
              new MyPromise((resolve, reject) => {
                resolve(
                  new MyPromise((resolve, reject) => {
                    resolve(100000000000)
                  })
                )
              })
            )
          })
        },
        (err) => console.log(err)
      )

      p2.then(
        (res) => {
          console.log(res)
        },
        (err) => {
          console.log(err)
        }
      )

  总结:实现源码需要解决的问题

      promise 源码~ 
    1. 异步的问题;
    2. 链式调用 => return this ?
    3.在初始化之前调用值的? 
    4. 递归的问题;

原文:https://www.cnblogs.com/mahuablog/p/15332816.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!