1. /*
    2. 第一步:
    3. 1. Promise 就是一个类 在执行这个类的时候 需要传递一个执行器进去 执行器会立即执行
    4. 2. Promise 中有三种状态 分别为 成功 fulfilled 失败 rejected 等待 pending
    5. pending -> fulfilled
    6. pending -> rejected
    7. 一旦状态确定就不可更改
    8. 3. resolve和reject函数是用来更改状态的
    9. resolve: fulfilled
    10. reject: rejected
    11. 4. then方法内部做的事情就判断状态 如果状态是成功 调用成功的回调函数 如果状态是失败 调用失败回调函数 then方法是被定义在原型对象中的
    12. 5. then成功回调有一个参数 表示成功之后的值 then失败回调有一个参数 表示失败后的原因
    13. 6. 同一个promise对象下面的then方法是可以被调用多次的
    14. 7. then方法是可以被链式调用的, 后面then方法的回调函数拿到值的是上一个then方法的回调函数的返回值
    15. */
    16. /*
    17. 第二步:
    18. 支持异步
    19. 将回调函数存储起来,并依次判断调用
    20. */
    21. /*
    22. 第三步:
    23. 支持链式调用
    24. */
    25. const PENDING = 'pending'; // 等待
    26. const FULFILLED = 'fulfilled'; // 成功
    27. const REJECTED = 'rejected'; // 失败
    28. class MyPromise {
    29. constructor(executor) {
    30. try {
    31. executor(this.resolve, this.reject)
    32. } catch (e) {
    33. this.reject(e);
    34. }
    35. }
    36. // promsie 状态
    37. status = PENDING;
    38. // 成功之后的值
    39. value = undefined;
    40. // 失败后的原因
    41. reason = undefined;
    42. // 成功回调
    43. successCallback = [];
    44. // 失败回调
    45. failCallback = [];
    46. resolve = value => {
    47. // 如果状态不是等待 阻止程序向下执行
    48. if (this.status !== PENDING) return;
    49. // 将状态更改为成功
    50. this.status = FULFILLED;
    51. // 保存成功之后的值
    52. this.value = value;
    53. // 判断成功回调是否存在 如果存在 调用
    54. // this.successCallback && this.successCallback(this.value);
    55. while (this.successCallback.length) this.successCallback.shift()()
    56. }
    57. reject = reason => {
    58. // 如果状态不是等待 阻止程序向下执行
    59. if (this.status !== PENDING) return;
    60. // 将状态更改为失败
    61. this.status = REJECTED;
    62. // 保存失败后的原因
    63. this.reason = reason;
    64. // 判断失败回调是否存在 如果存在 调用
    65. // this.failCallback && this.failCallback(this.reason);
    66. while (this.failCallback.length) this.failCallback.shift()()
    67. }
    68. then(successCallback, failCallback) {
    69. // 参数可选
    70. successCallback = successCallback ? successCallback : value => value;
    71. // 参数可选
    72. failCallback = failCallback ? failCallback : reason => { throw reason };
    73. let promsie2 = new MyPromise((resolve, reject) => {
    74. // 判断状态
    75. if (this.status === FULFILLED) {
    76. setTimeout(() => {
    77. try {
    78. let x = successCallback(this.value);
    79. // 判断 x 的值是普通值还是promise对象
    80. // 如果是普通值 直接调用resolve
    81. // 如果是promise对象 查看promsie对象返回的结果
    82. // 再根据promise对象返回的结果 决定调用resolve 还是调用reject
    83. resolvePromise(promsie2, x, resolve, reject)
    84. } catch (e) {
    85. reject(e);
    86. }
    87. }, 0)
    88. } else if (this.status === REJECTED) {
    89. setTimeout(() => {
    90. try {
    91. let x = failCallback(this.reason);
    92. // 判断 x 的值是普通值还是promise对象
    93. // 如果是普通值 直接调用resolve
    94. // 如果是promise对象 查看promsie对象返回的结果
    95. // 再根据promise对象返回的结果 决定调用resolve 还是调用reject
    96. resolvePromise(promsie2, x, resolve, reject)
    97. } catch (e) {
    98. reject(e);
    99. }
    100. }, 0)
    101. } else {
    102. // 等待
    103. // 将成功回调和失败回调存储起来
    104. this.successCallback.push(() => {
    105. setTimeout(() => {
    106. try {
    107. let x = successCallback(this.value);
    108. // 判断 x 的值是普通值还是promise对象
    109. // 如果是普通值 直接调用resolve
    110. // 如果是promise对象 查看promsie对象返回的结果
    111. // 再根据promise对象返回的结果 决定调用resolve 还是调用reject
    112. resolvePromise(promsie2, x, resolve, reject)
    113. } catch (e) {
    114. reject(e);
    115. }
    116. }, 0)
    117. });
    118. this.failCallback.push(() => {
    119. setTimeout(() => {
    120. try {
    121. let x = failCallback(this.reason);
    122. // 判断 x 的值是普通值还是promise对象
    123. // 如果是普通值 直接调用resolve
    124. // 如果是promise对象 查看promsie对象返回的结果
    125. // 再根据promise对象返回的结果 决定调用resolve 还是调用reject
    126. resolvePromise(promsie2, x, resolve, reject)
    127. } catch (e) {
    128. reject(e);
    129. }
    130. }, 0)
    131. });
    132. }
    133. });
    134. return promsie2;
    135. }
    136. finally(callback) {
    137. return this.then(value => {
    138. return MyPromise.resolve(callback()).then(() => value);
    139. }, reason => {
    140. return MyPromise.resolve(callback()).then(() => { throw reason })
    141. })
    142. }
    143. catch(failCallback) {
    144. return this.then(undefined, failCallback)
    145. }
    146. static all(array) {
    147. let result = [];
    148. let index = 0;
    149. return new MyPromise((resolve, reject) => {
    150. function addData(key, value) {
    151. result[key] = value;
    152. index++;
    153. if (index === array.length) {
    154. resolve(result);
    155. }
    156. }
    157. for (let i = 0; i < array.length; i++) {
    158. let current = array[i];
    159. if (current instanceof MyPromise) {
    160. // promise 对象
    161. current.then(value => addData(i, value), reason => reject(reason))
    162. } else {
    163. // 普通值
    164. addData(i, array[i]);
    165. }
    166. }
    167. })
    168. }
    169. static resolve(value) {
    170. if (value instanceof MyPromise) return value;
    171. return new MyPromise(resolve => resolve(value));
    172. }
    173. }
    174. function resolvePromise(promsie2, x, resolve, reject) {
    175. if (promsie2 === x) {
    176. return reject(new TypeError('Chaining cycle detected for promise #<Promise>'))
    177. }
    178. if (x instanceof MyPromise) {
    179. // promise 对象
    180. // x.then(value => resolve(value), reason => reject(reason));
    181. x.then(resolve, reject);
    182. } else {
    183. // 普通值
    184. resolve(x);
    185. }
    186. }
    187. module.exports = MyPromise;