美文网首页
实现一个简单的promise

实现一个简单的promise

作者: 学不动了Zzz | 来源:发表于2021-03-23 15:56 被阅读0次
    const Fulfilled = 'fulfilled'
    const Rejected = 'rejected'
    const Pending = 'pending'
    const promiseError = val => `(in promise) ${val}`
    
    function P(fn) {
      this.state = Pending
      this.value = undefined
      this.onFulfilledCb = []
      this.onRejectedCb = []
    
      const resolve = val => {
        if (this.state === Pending) {
          this.state = Fulfilled
          this.value = val
    
          while (this.onFulfilledCb.length) {
            const fulfiledFn = this.onFulfilledCb.shift()
            fulfiledFn()
          }
        }
      }
    
      const reject = val => {
        if (this.state === Pending) {
          this.state = Rejected
          this.value = val
    
          // if (!this.onRejectedCb.length) {
          //   throw promiseError(val)
          // }
    
          while (this.onRejectedCb.length) {
            const rejectedFn = this.onRejectedCb.shift()
            rejectedFn()
          }
        }
      }
    
      fn(resolve, reject)
    }
    
    P.prototype.then = function(onFulfilled, onRejected) {
      onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : v => v
      onRejected = typeof onRejected === 'function' ? onRejected : r => r
    
      return new P((resolve, reject) => {
        const fulfilledHandler = () => {
          try {
            resolve(onFulfilled(this.value))
          } catch (e) {
            reject(e)
          }
        }
    
        const rejectedHandler = () => {
          // if (typeof onRejected === 'function') {
            try {
              resolve(onRejected(this.value))
            } catch (e) {
              reject(e)
            }
          // } else {
          //   throw promiseError(this.value)
          // }
        }
    
        if (this.state === Pending) {
          this.onFulfilledCb.push(fulfilledHandler)
    
          this.onRejectedCb.push(rejectedHandler)
         }
    
        if (this.state === Fulfilled) {
          fulfilledHandler()
        }
    
        if (this.state === Rejected) {
          rejectedHandler()
        }
      })
    }
    
    P.prototype.catch = function(callback) {
      return this.then(undefined, callback)
    }
    
    P.prototype.finally = function (callback) {
      return this.then(callback, undefined)
    }
    
    P.resolve = function(val) {
      return new P(resolve => resolve(val))
    }
    
    P.reject = function(val) {
      return new P((_, reject) => reject(val))
    }
    
    P.all = function(arr) {
      return new P((resolve, reject) => {
        const values = []
        let count = 0
        for (let [i, p] of arr.entries()) {
          p.then(res => {
            values[i] = res
            count++
    
            if (count === arr.length) {
              resolve(values)
            }
          }, err => {
            reject(err)
          })
        }
      })
    }
    
    P.race = function(arr) {
      return new P((resolve, reject) => {
        for (let p of arr) {
          p.then(res => {
            resolve(res)
          }, err => {
            reject(err)
          })
        }
      })
    }
    
    

    相关文章

      网友评论

          本文标题:实现一个简单的promise

          本文链接:https://www.haomeiwen.com/subject/mmpfhltx.html