typeScript版仿Promise源码

2022-02-07  本文已影响0人  DeadMoon

作为一个前端, 还有人说不会用 Promise,真的就有点过分了. 为了跟上卷的潮流,我们今天就用typeScript 来实现一下 Promise 的实现.
*写 promise 之前我们需要明确一些常识.

下面我们开始进入代码块
// 首先promise 有三个状态, 我们先用常量定义一下
const PADDING: string = 'padding' // 等待
const FULFILLED: string = 'fulfilled' // 完成
const REJECT: string = 'reject' // 拒绝
type callbackType = { (val: any): any }

class CustomPromise {
 status = PADDING
 value: any = ''
 reason: any = ''
 // 调用 new 关键字时执行
 constructor(execute: { (fn1: callbackType, fn2: callbackType): any }) {
   execute(this.resolve.bind(this), this.reject.bind(this))
 }
 resolve(value: any) {
   const { status } = this
   if (status !== PADDING) return  // promise 状态一旦凝固, 将不在发生改变, 所以此处做了判断
   this.status = FULFILLED // padding状态改变状态
   this.value = value // 将 resolve 值存储起来,在 then 回调时使用
 }
 // 同 resolve
 reject(reason: any) {
   const { status } = this
   if (status !== PADDING) return
   this.status = REJECT
   this.reason = reason
 }
 // promise 使用 then 接受两个回调函数
 then(fulCallback: callbackType, failCallback?: callbackType) {
   const { status, value, reason } = this
   if (status === FULFILLED) {
     fulCallback(value) // 通过 callback 在回调函数里再获取 value 值
   }
   // 同 resolve
   if (status === REJECT) {
     failCallback!(reason)
   }
 }
}

以上我们就实现了一个很简单的 Promise, 这个 Promise 很弱, 只能执行同步代码😭, 也不支持链式调用.显然这样肯定不行的, 大家都知道, Promise的提出就是为了解决异步回调地狱问题, 下面我们就来加入异步的处理.
*异步处理我们大致解决方案是

  const PADDING: string = 'padding' // 等待
  const FULFILLED: string = 'fulfilled' // 完成
  const REJECT: string = 'reject' // 拒绝
  type callbackType = { (val: any): any }

  class CustomPromise {
    status = PADDING
    value: any = ''
    reason: any = ''
+  fulCallbackList: Function[] = []
+  failCallbackList: Function[] = []
    // 调用 new 关键字时执行
    constructor(execute: { (fn1: callbackType, fn2: callbackType): any }) {
      execute(this.resolve.bind(this), this.reject.bind(this))
    }
    resolve(value: any) {
      const { status, fulCallbackList } = this
      if (status !== PADDING) return  // promise 状态一旦凝固, 将不在发生改变, 所以此处做了判断
      this.status = FULFILLED // padding状态改变状态
      this.value = value // 将 resolve 值存储起来,在 then 回调时使用
      // ② 如果异步了, fulCallbackList 队列存放的就是.then 的回调函数
    +  fulCallbackList.forEach(fn => fn())
    }
    // 同 resolve
    reject(reason: any) {
      const { status, failCallbackList } = this
      if (status !== PADDING) return
      this.status = REJECT
      this.reason = reason
    +  failCallbackList.forEach(fn => fn())
    }
    // promise 使用 then 接受两个回调函数
    then(fulCallback: callbackType, failCallback?: callbackType) {
      const { status, value, reason } = this
      if (status === FULFILLED) {
        fulCallback(value) // 通过 callback 在回调函数里再获取 value 值
      }
      // 同 resolve
      if (status === REJECT) {
        failCallback!(reason)
      }
      // ① 异步处理
     + if (status === PADDING) { // 将回调函数放到队列里,在状态发生改变时再调用
     +   this.fulCallbackList.push(() => {
     +     fulCallback(value)
     +    })
     +   this.failCallbackList.push(() => {
     +    failCallback!(reason)
     +  })
     + }  
    }
  }

*以上我们解决了异步问题.今天更新到此为止, 下面有时间再更新一下 .then 的链式调用

上一篇 下一篇

猜你喜欢

热点阅读