// 设置成常量的目的
// 1.方便阅读
// 2.不容易写错
const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
class Promise {
  constructor(executor) {
    try {
      executor(this.resolve, this.reject)
    } catch (err) {
      this.reject(err)
    }
  }

  status = PENDING
  value = null
  reason = null
  fulfilledCallbacks = []
  rejectedCallbacks = []

  resolve(value) {
    // 1.改变状态
    // 2.执行成功回调
    if (this.status == PENDING) {
      this.status = FULFILLED
      this.value = value
      while (this.fulfilledCallbacks.length) {
        this.fulfilledCallbacks.shift()(value)
      }
    }
  }

  reject(reason) {
    // 1.改变状态
    // 2.执行失败回调
    if (this.status == PENDING) {
      this.status = REJECTED
      this.reason = reason
      while (this.rejectedCallbacks.length) {
        this.rejectedCallbacks.shift()(reason)
      }
    }
  }

  then(onFulfilled, onRejected) {
    // 1.格式化参数
    // 2.返回新的promise
    onFulfilled =
      typeof onFulfilled == 'function' ? onFulfilled : (value) => value
    onRejected =
      typeof onRejected == 'function' ? onRejected : (reason) => throw reason

    const promise2 = new Promise((resolve, reject) => {
      // 1.将回调函数封装成微任务 => 为了等待promise2初始化完成
      // 2.根据状态加入对应的回调队列
      const onFulfilledMicrotask = () =>
        queueMicrotask(() => {
          try {
            const x = onFulfilled(this.value)
            resolvePromise(promise2, x, resolve, reject)
          } catch (err) {
            reject(err)
          }
        })
      const onRejectedMicrotask = () =>
        queueMicrotask(() => {
          try {
            const x = onRejected(this.reason)
            resolvePromise(promise2, x, resolve, reject)
          } catch (err) {
            reject(err)
          }
        })

      if (this.status == FULFILLED) {
        onFulfilledMicrotask()
      } else if (this.status == REJECTED) {
        onRejectedMicrotask()
      } else if (this.status == PENDING) {
        this.fulfilledCallbacks.push(onFulfilledMicrotask)
        this.rejectedCallbacks.push(onRejectedMicrotask)
      }
    })
    return promise2
  }

  static resolve(value) {
    if (value instanceof Promise) {
      return value
    }
    return new Promise((resolve, reject) => {
      resolve(value)
    })
  }

  static reject(reason) {
    return new Promise((resolve, reject) => {
      reject(reason)
    })
  }
}
function resolvePromise(promise2, x, resolve, reject) {
  // 1.不可返回自身promise
  if (promise2 == x) {
    throw reject(new TypeError(x + '未初始化完成'))
  }
  // 2.如果返回其它promise,状态改变后执行promise2的resolve和reject函数
  if (x instanceof Promise) {
    x.then(resolve, reject)
  } else {
    resolve(x)
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
最近更新: 4 小时前