Promise代码

 Promise构造函数
      Promise状态 status pending  fulfilled rejected
      const PENDING = 'pending'
      const FULFILLED = 'fulfilled'
      const REJECTED = 'rejected'
      class Promise {
        //  实例属性
        status = PENDING
        result = undefined
        //  私有属性 this.#handlers = []
        #handles = []
        constructor(executor) {
          // console.log(executor)
          // console.log('构造函数执行了')
          const resolve = (result) => {
            if (this.status === PENDING) {
              this.status = FULFILLED
              this.result = result
              // 把之前存的函数依次取出来调用
              this.#handles.forEach(({ onFulfilled }) => {
                onFulfilled(this.result)
              })
            }
          }
          const reject = (result) => {
            if (this.status === PENDING) {
              this.status = REJECTED
              this.result = result
              // 把之前存的函数依次取出来调用
              this.#handles.forEach(({ onRejected }) => {
                onRejected(this.result)
              })
            }
          }
          executor(resolve, reject)
        }

        then(onFulfilled, onRejected) {
          onFulfilled =
            typeof onFulfilled === 'function' ? onFulfilled : (x) => x
          onRejected =
            typeof onRejected === 'function'
              ? onRejected
              : (x) => {
                  throw x
                }
          // 判断状态
          if (this.status === FULFILLED) {
            onFulfilled(this.result)
          } else if (this.status === REJECTED) {
            onRejected(this.result)
          } else {
            // 调用then的时候,状态是默认状态,把传入的两个函数存储起来
            // [{onFulfilled, onRejected},{OnFulfilled, onRejected}]
            this.#handles.push({
              onFulfilled,
              onRejected,
            })
          }
        }
      }

      /**/ console.log('start')
      const p = new Promise((resolve, reject) => {
        console.log(11)
        resolve('ok')
        //reject('出错了')
        // setTimeout(() => {
        //   console.log(123)
        // }, 1000)
        // setTimeout(() => {
        //   resolve('ok')
        // }, 1000)
      })

      // then可以调用多次
      p.then(
        (data) => {
          console.log(data)
        },
        (reason) => {
          console.log(reason)
        }
      )

      /*  p.then(
        (data) => {
          console.log(data)
        },
        (reason) => {
          console.log(reason)
        }
      ) */
      // console.log(p.status, p.result)
      //console.log(p.handles)
      console.log('end')

      // const p1 = new Promise((resolve, reject) => {
      //   //resolve(100)
      //   reject(123)
      // })
      //p1.then(30)

.手撕Promise-then变为异步

function runAsyncTask(callback) {

        // queueMicroTask() ->  MutationObserver  -> setTimeout()

        if (typeof queueMicrotask === 'function') {

          queueMicrotask(callback)

        } else if (typeof MutationObserver === 'function') {

          const mutationObserver = new MutationObserver(callback)

          const oDiv = document.createElement('div')

          mutationObserver.observe(oDiv, { childList: true })

          oDiv.innerText = 'longge666'

        } else {

          setTimeout(callback, 0)

        }

      }

      // Promise构造函数

      // Promise状态 status pending  fulfilled rejected

      const PENDING = 'pending'

      const FULFILLED = 'fulfilled'

      const REJECTED = 'rejected'

      class Promise {

        //  实例属性

        status = PENDING

        result = undefined

        //  私有属性 this.#handlers = []

        #handles = []

        constructor(executor) {

          // console.log(executor)

          // console.log('构造函数执行了')

          const resolve = (result) => {

            if (this.status === PENDING) {

              this.status = FULFILLED

              this.result = result

              // 把之前存的函数依次取出来调用

              this.#handles.forEach(({ onFulfilled }) => {

                onFulfilled()

              })

            }

          }

          const reject = (result) => {

            if (this.status === PENDING) {

              this.status = REJECTED

              this.result = result

              // 把之前存的函数依次取出来调用

              this.#handles.forEach(({ onRejected }) => {

                onRejected()

              })

            }

          }

          executor(resolve, reject)

        }

        then(onFulfilled, onRejected) {

          onFulfilled =

            typeof onFulfilled === 'function' ? onFulfilled : (x) => x

          onRejected =

            typeof onRejected === 'function'

              ? onRejected

              : (x) => {

                  throw x

                }

          // 判断状态

          if (this.status === FULFILLED) {

            runAsyncTask(() => {

              onFulfilled(this.result)

            })

          } else if (this.status === REJECTED) {

            runAsyncTask(() => {

              onRejected(this.result)

            })

          } else {

            // 调用then的时候,状态是默认状态,把传入的两个函数存储起来

            // [{onFulfilled, onRejected},{OnFulfilled, onRejected}]

            this.#handles.push({

              onFulfilled: () => {

                runAsyncTask(() => {

                  onFulfilled(this.result)

                })

              },

              onRejected: () => {

                runAsyncTask(() => {

                  onRejected(this.result)

                })

              },

            })

          }

        }

      }

      /**/ console.log('start')

      const p = new Promise((resolve, reject) => {

        console.log(11)

        ///resolve('ok')

        //reject('出错了')

        // setTimeout(() => {

        //   console.log(123)

        // }, 1000)

        setTimeout(() => {

          resolve('ok')

        }, 1000)

      })

      // then可以调用多次

      p.then(

        (data) => {

          console.log(data)

        },

        (reason) => {

          console.log(reason)

        }

      )

      console.log('end')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值