1. class MyPromise {
    2. constructor (exe) {
    3. // 最后的值,Promise .then或者.catch接收的值
    4. this.value = undefined
    5. // 状态:三种状态 pending success failure
    6. this.status = 'pending'
    7. // 成功的函数队列
    8. this.successQueue = []
    9. // 失败的函数队列
    10. this.failureQueue = []
    11. const resolve = (value) => {
    12. const doResolve = () => {
    13. // 将缓存的函数队列挨个执行,并且将状态和值设置好
    14. if (this.status === 'pending') {
    15. this.status = 'success'
    16. this.value = value
    17. while (this.successQueue.length) {
    18. const cb = this.successQueue.shift()
    19. cb && cb(this.value)
    20. }
    21. }
    22. }
    23. setTimeout(doResolve, 0)
    24. }
    25. const reject = (value) => {
    26. // 基本同resolve
    27. const doReject = () => {
    28. if (this.status === 'pending') {
    29. this.status = 'failure'
    30. this.value = value
    31. while (this.failureQueue.length) {
    32. const cb = this.failureQueue.shift()
    33. cb && cb(this.value)
    34. }
    35. }
    36. }
    37. setTimeout(doReject, 0)
    38. }
    39. exe(resolve, reject)
    40. }
    41. then (success = (value) => value, failure = (value) => value) {
    42. // .then返回的是一个新的Promise
    43. return new MyPromise((resolve, reject) => {
    44. // 包装回到函数
    45. const successFn = (value) => {
    46. try {
    47. const result = success(value)
    48. // 如果结果值是一个Promise,那么需要将这个Promise的值继续往下传递,否则直接resolve即可
    49. result instanceof MyPromise ? result.then(resolve, reject) : resolve(result)
    50. } catch (err) {
    51. reject(err)
    52. }
    53. }
    54. // 基本筒成功回调函数的封装
    55. const failureFn = (value) => {
    56. try {
    57. const result = failure(value)
    58. result instanceof MyPromise ? result.then(resolve, reject) : resolve(result)
    59. } catch (err) {
    60. reject(err)
    61. }
    62. }
    63. // 如果Promise的状态还未结束,则将成功和失败的函数缓存到队列里
    64. if (this.status === 'pending') {
    65. this.successQueue.push(successFn)
    66. this.failureQueue.push(failureFn)
    67. // 如果已经成功结束,直接执行成功回调
    68. } else if (this.status === 'success') {
    69. success(this.value)
    70. } else {
    71. // 如果已经失败,直接执行失败回调
    72. failure(this.value)
    73. }
    74. })
    75. }
    76. // 其他函数就不一一实现了
    77. catch () {
    78. }
    79. }
    80. // 以下举个例子,验证一下以上实现的结果
    81. const pro = new MyPromise((resolve, reject) => {
    82. setTimeout(resolve, 1000)
    83. setTimeout(reject, 2000)
    84. })
    85. pro
    86. .then(() => {
    87. console.log('2_1')
    88. const newPro = new MyPromise((resolve, reject) => {
    89. console.log('2_2')
    90. setTimeout(reject, 2000)
    91. })
    92. console.log('2_3')
    93. return newPro
    94. })
    95. .then(
    96. () => {
    97. console.log('2_4')
    98. },
    99. () => {
    100. console.log('2_5')
    101. }
    102. )
    103. pro
    104. .then(
    105. data => {
    106. console.log('3_1')
    107. throw new Error()
    108. },
    109. data => {
    110. console.log('3_2')
    111. }
    112. )
    113. .then(
    114. () => {
    115. console.log('3_3')
    116. },
    117. e => {
    118. console.log('3_4')
    119. }
    120. )
    121. // 2_1
    122. // 2_2
    123. // 2_3
    124. // 3_1
    125. // 3_4
    126. // 2_5