手写一个符合promise A+规范的promsie

class MyPromise {
  constructor (executor) {
    this.state = 'pending'
    this.value = this.reason = undefined
    this.onFulfilledCallback = []
    this.onRejectedCallback = []

    try {
      executor(this.resolve, this.reject)
    } catch (error) {
      this.reject(error)
    }
  }

  resolve = value => {
    if (this.state === 'pending') {
      this.state = 'fulfilled'
      this.value = value
      this.onFulfilledCallback.forEach(cb => cb())
    }
  }

  reject = reason => {
    if (this.state === 'pending') {
      this.state = 'rejected'
      this.reason = reason
      this.onRejectedCallback.forEach(cb => cb())
    }
  }

  then = (onFulfilled, onRejected) => {
    onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : v => v
    onRejected = typeof onRejected === 'function' ? onRejected : r => { throw r }

    const promise2 = new MyPromise((resolve, reject) => {
      if (this.state === 'fulfilled') {
        setTimeout(() => {
          try {
            const x = onFulfilled(this.value)
            resolvePromise(promise2, x, resolve, reject);
          } catch (error) {
            reject(error)
          }
        }, 0)
      }

      if (this.state === 'rejected') {
        setTimeout(() => {
          try {
            const x = onRejected(this.reason)
            resolvePromise(promise2, x, resolve, reject);
          } catch (error) {
            reject(error)
          }
        }, 0)
      }

      if (this.state === 'pending') {
        this.onFulfilledCallback.push(() => {
          setTimeout(() => {
            try {
              const x = onFulfilled(this.value)
              resolvePromise(promise2, x, resolve, reject);
            } catch (error) {
              reject(error)
            }
          }, 0)
        })
        this.onRejectedCallback.push(() => {
          setTimeout(() => {
            try {
              const x = onRejected(this.reason)
              resolvePromise(promise2, x, resolve, reject);
            } catch (error) {
              reject(error)
            }
          })
        })
      }
    })

    return promise2
  }

  catch = fn => this.then(void 0, fn)
}

MyPromise.resolve = value => new MyPromise(resolve => resolve(value))

MyPromise.reject = reason => new MyPromise((resolve, reject) => reject(reason))

MyPromise.race = fnArr => {
  return new MyPromise((resolve, reject) => {
    fnArr.forEach(fn => {
      fn().then(resolve, reject)
    })
  })
}

MyPromise.all = fnArr => {
  const result = []
  const index = 0
  return new MyPromise((resolve, reject) => {
    fnArr.forEach((fn, i) => {
      fn().then((v) => {
        result[i] = v
        index++
        if (index === fnArr.length) {
          resolve(result)
        }
      }, reject)
    })
  })
}

function resolvePromise (promise2, x, resolve, reject) {
  if (x === promise2) {
    reject(new TypeError('Chaining cycle detected for promise'))
  }

  // 锁
  let called

  if (x != null && (typeof x === 'object' || typeof x === 'function')) {
    try {
      const then = x.then
      if (typeof then === 'function') {
        then.call(x, y => {
          if (called) return
          called = true
          resolvePromise(promise2, y, resolve, reject)
        }, err => {
          if (called) return
          called = true
          reject(err)
        })
      } else {
        resolve(x)
      }
    } catch (error) {
      if (called) return
      called = true
      reject(error)
    }
  } else {
    resolve(x)
  }
}

你可能感兴趣的:(手写一个符合promise A+规范的promsie)