1. class Promise {
    2. // 构造方法
    3. constructor(executor) {
    4. // 状态
    5. this.promiseState = 'pending'
    6. // 结果值
    7. this.promiseResult = null
    8. // 用来保存当前的 this
    9. const _this = this
    10. // 保存 then 中的回调函数
    11. this.callback = []
    12. // 定义 resove 和 reject 函数
    13. function resolve (data) {
    14. // 判断状态
    15. if (_this.promiseState !== 'pending') return
    16. // 1.修改对象的状态(promiseState)
    17. _this.promiseState = 'fulfilled'
    18. // 2.设置对象的结果
    19. _this.promiseResult = data
    20. // 执行 callback 中的函数
    21. setTimeout(() => {
    22. _this.callback.forEach(item => {
    23. item.onResolved(data)
    24. })
    25. })
    26. }
    27. function reject (err) {
    28. // 判断状态
    29. if (_this.promiseState !== 'pending') return
    30. // 1.修改对象的状态(promiseState)
    31. _this.promiseState = 'rejected'
    32. // 2.设置对象的结果
    33. _this.promiseResult = err
    34. // 执行 callback 中的函数
    35. setTimeout(() => {
    36. _this.callback.forEach(item => {
    37. item.onRejected(err)
    38. })
    39. })
    40. }
    41. // 同步调用执行器函数,并且执行器接收两个函数作为参数
    42. try {
    43. executor(resolve, reject)
    44. } catch (error) {
    45. _this.promiseState = 'rejected'
    46. _this.promiseResult = error
    47. }
    48. }
    49. // then 方法
    50. then (onResolved, onRejected) {
    51. const _this = this
    52. // 判断回调函数参数
    53. if (typeof onResolved !== 'function') {
    54. onResolved = value => value
    55. }
    56. if (typeof onRejected !== 'function') {
    57. onRejected = reason => {
    58. throw reason
    59. }
    60. }
    61. // 返回一个 Promise 实例
    62. return new Promise((resolve, reject) => {
    63. function callback (type) {
    64. try {
    65. // 保存回调的结果
    66. let result = type(_this.promiseResult)
    67. // 判断回调返回的是什么
    68. if (result instanceof Promise) {
    69. // 如果返回的是一个 Promise,状态和结果都由 result 决定
    70. result.then(value => {
    71. resolve(value)
    72. }, reason => {
    73. reject(reason)
    74. })
    75. } else {
    76. // 如果返回 Promise 以外的,状态都为 fulfilled
    77. resolve(result)
    78. }
    79. } catch (error) {
    80. reject(error)
    81. }
    82. }
    83. if (this.promiseState === 'pending') {
    84. // 将回调压入进数组中
    85. this.callback.push({
    86. onResolved: function () {
    87. callback(onResolved)
    88. },
    89. onRejected: function () {
    90. callback(onRejected)
    91. }
    92. })
    93. }
    94. if (this.promiseState === 'fulfilled') {
    95. setTimeout(() => {
    96. callback(onResolved)
    97. })
    98. }
    99. if (this.promiseState === 'rejected') {
    100. setTimeout(() => {
    101. callback(onRejected)
    102. })
    103. }
    104. })
    105. }
    106. // catch 方法
    107. catch (onRejected) {
    108. return this.then(undefined, onRejected)
    109. }
    110. // resolve 方法
    111. static resolve (argument) {
    112. return new Promise((resolve, reject) => {
    113. if (argument instanceof Promise) {
    114. argument.then(value => {
    115. resolve(value)
    116. }, reason => {
    117. reject(reason)
    118. })
    119. } else {
    120. resolve(argument)
    121. }
    122. })
    123. }
    124. // reject 方法
    125. static reject (reason) {
    126. return new Promise((resolve, reject) => {
    127. reject(reason)
    128. })
    129. }
    130. // all 方法
    131. static all (promises) {
    132. return new Promise((resolve, reject) => {
    133. // 用于存放成功的 promise
    134. let count = 0
    135. // 用于存放成功 promise 的值
    136. let arr = []
    137. for (let i = 0; i < promises.length; i++) {
    138. promises[i].then(value => {
    139. count++
    140. count[i] = value
    141. // 如果 promise 成功的数量与数组长度相同时,则表示全部成功了
    142. if (count == promises.length) {
    143. resolve(arr)
    144. }
    145. }, reason => {
    146. reject(reason)
    147. })
    148. }
    149. })
    150. }
    151. // race 方法
    152. static race (promises) {
    153. return new Promise((resolve, reject) => {
    154. for (let i = 0; i < promises.length; i++) {
    155. promises[i].then(value => {
    156. // 修改返回对象的状态为 成功
    157. resolve(value)
    158. }, reason => {
    159. // 修改返回对象的状态为 失败
    160. reject(reason)
    161. })
    162. }
    163. })
    164. }
    165. }