1. Promise 就是一个类 在执行这个类的时候 需要传递一个执行器进去 执行器会立即执行
      2. Promise 中有三种状态 分别为 成功 fulfilled 失败 rejected 等待 pending
      pending -> fulfilled
      pending -> rejected
      一旦状态确定就不可更改
      3. resolve和reject函数是用来更改状态的
      resolve: fulfilled
      reject: rejected
      4. then方法内部做的事情就判断状态 如果状态是成功 调用成功的回调函数 如果状态是失败 调用失败回调函数 then方法是被定义在原型对象中的
      5. then成功回调有一个参数 表示成功之后的值 then失败回调有一个参数 表示失败后的原因
      6. 同一个promise对象下面的then方法是可以被调用多次的
      7. then方法是可以被链式调用的, 后面then方法的回调函数拿到值的是上一个then方法的回调函数的返回值 ```javascript const MyPromise = require(‘./myPromise’);

    function p1 () { return new MyPromise(function (resolve, reject) { setTimeout(function () { resolve(‘p1’) }, 2000) }) } function p2 () { return new MyPromise(function (resolve, reject) { reject(‘失败’) // resolve(‘成功’);
    }) }

    p2() .then(value => console.log(value)) .catch(reason => console.log(reason))

    1. ```javascript
    2. const PENDING = 'pending'; // 等待
    3. const FULFILLED = 'fulfilled'; // 成功
    4. const REJECTED = 'rejected'; // 失败
    5. class MyPromise {
    6. constructor (executor) {
    7. try {
    8. executor(this.resolve, this.reject)
    9. } catch (e) {
    10. this.reject(e);
    11. }
    12. }
    13. // promsie 状态
    14. status = PENDING;
    15. // 成功之后的值
    16. value = undefined;
    17. // 失败后的原因
    18. reason = undefined;
    19. // 成功回调
    20. successCallback = [];
    21. // 失败回调
    22. failCallback = [];
    23. resolve = value => {
    24. // 如果状态不是等待 阻止程序向下执行
    25. if (this.status !== PENDING) return;
    26. // 将状态更改为成功
    27. this.status = FULFILLED;
    28. // 保存成功之后的值
    29. this.value = value;
    30. // 判断成功回调是否存在 如果存在 调用
    31. // this.successCallback && this.successCallback(this.value);
    32. while(this.successCallback.length) this.successCallback.shift()()
    33. }
    34. reject = reason => {
    35. // 如果状态不是等待 阻止程序向下执行
    36. if (this.status !== PENDING) return;
    37. // 将状态更改为失败
    38. this.status = REJECTED;
    39. // 保存失败后的原因
    40. this.reason = reason;
    41. // 判断失败回调是否存在 如果存在 调用
    42. // this.failCallback && this.failCallback(this.reason);
    43. while(this.failCallback.length) this.failCallback.shift()()
    44. }
    45. then (successCallback, failCallback) {
    46. // 参数可选
    47. successCallback = successCallback ? successCallback : value => value;
    48. // 参数可选
    49. failCallback = failCallback ? failCallback: reason => { throw reason };
    50. let promsie2 = new MyPromise((resolve, reject) => {
    51. // 判断状态
    52. if (this.status === FULFILLED) {
    53. setTimeout(() => {
    54. try {
    55. let x = successCallback(this.value);
    56. // 判断 x 的值是普通值还是promise对象
    57. // 如果是普通值 直接调用resolve
    58. // 如果是promise对象 查看promsie对象返回的结果
    59. // 再根据promise对象返回的结果 决定调用resolve 还是调用reject
    60. resolvePromise(promsie2, x, resolve, reject)
    61. }catch (e) {
    62. reject(e);
    63. }
    64. }, 0)
    65. }else if (this.status === REJECTED) {
    66. setTimeout(() => {
    67. try {
    68. let x = failCallback(this.reason);
    69. // 判断 x 的值是普通值还是promise对象
    70. // 如果是普通值 直接调用resolve
    71. // 如果是promise对象 查看promsie对象返回的结果
    72. // 再根据promise对象返回的结果 决定调用resolve 还是调用reject
    73. resolvePromise(promsie2, x, resolve, reject)
    74. }catch (e) {
    75. reject(e);
    76. }
    77. }, 0)
    78. } else {
    79. // 等待
    80. // 将成功回调和失败回调存储起来
    81. this.successCallback.push(() => {
    82. setTimeout(() => {
    83. try {
    84. let x = successCallback(this.value);
    85. // 判断 x 的值是普通值还是promise对象
    86. // 如果是普通值 直接调用resolve
    87. // 如果是promise对象 查看promsie对象返回的结果
    88. // 再根据promise对象返回的结果 决定调用resolve 还是调用reject
    89. resolvePromise(promsie2, x, resolve, reject)
    90. }catch (e) {
    91. reject(e);
    92. }
    93. }, 0)
    94. });
    95. this.failCallback.push(() => {
    96. setTimeout(() => {
    97. try {
    98. let x = failCallback(this.reason);
    99. // 判断 x 的值是普通值还是promise对象
    100. // 如果是普通值 直接调用resolve
    101. // 如果是promise对象 查看promsie对象返回的结果
    102. // 再根据promise对象返回的结果 决定调用resolve 还是调用reject
    103. resolvePromise(promsie2, x, resolve, reject)
    104. }catch (e) {
    105. reject(e);
    106. }
    107. }, 0)
    108. });
    109. }
    110. });
    111. return promsie2;
    112. }
    113. finally (callback) {
    114. return this.then(value => {
    115. return MyPromise.resolve(callback()).then(() => value);
    116. }, reason => {
    117. return MyPromise.resolve(callback()).then(() => { throw reason })
    118. })
    119. }
    120. catch (failCallback) {
    121. return this.then(undefined, failCallback)
    122. }
    123. static all (array) {
    124. let result = [];
    125. let index = 0;
    126. return new MyPromise((resolve, reject) => {
    127. function addData (key, value) {
    128. result[key] = value;
    129. index++;
    130. if (index === array.length) {
    131. resolve(result);
    132. }
    133. }
    134. for (let i = 0; i < array.length; i++) {
    135. let current = array[i];
    136. if (current instanceof MyPromise) {
    137. // promise 对象
    138. current.then(value => addData(i, value), reason => reject(reason))
    139. }else {
    140. // 普通值
    141. addData(i, array[i]);
    142. }
    143. }
    144. })
    145. }
    146. static resolve (value) {
    147. if (value instanceof MyPromise) return value;
    148. return new MyPromise(resolve => resolve(value));
    149. }
    150. }
    151. function resolvePromise (promsie2, x, resolve, reject) {
    152. if (promsie2 === x) {
    153. return reject(new TypeError('Chaining cycle detected for promise #<Promise>'))
    154. }
    155. if (x instanceof MyPromise) {
    156. // promise 对象
    157. // x.then(value => resolve(value), reason => reject(reason));
    158. x.then(resolve, reject);
    159. } else {
    160. // 普通值
    161. resolve(x);
    162. }
    163. }
    164. module.exports = MyPromise;