1. class Promise {
    2. constructor(executor) {
    3. this.state = 'pending';
    4. this.value = undefined;
    5. this.reason = undefined;
    6. this.onResolvedCallbacks = [];
    7. this.onRejectedCallbacks = [];
    8. let resolve = value => {
    9. if (this.state === 'pending') {
    10. this.state = 'fulfilled';
    11. this.value = value;
    12. this.onResolvedCallbacks.forEach(fn => fn());
    13. }
    14. };
    15. let reject = reason => {
    16. if (this.state === 'pending') {
    17. this.state = 'rejected';
    18. this.reason = reason;
    19. this.onRejectedCallbacks.forEach(fn => fn());
    20. }
    21. };
    22. try {
    23. executor(resolve, reject);
    24. } catch (err) {
    25. reject(err);
    26. }
    27. }
    28. then(onFulfilled, onRejected) {
    29. onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value;
    30. onRejected = typeof onRejected === 'function' ? onRejected : err => { throw err };
    31. let promise2 = new Promise((resolve, reject) => {
    32. if (this.state === 'fulfilled') {
    33. setTimeout(() => {
    34. try {
    35. let x = onFulfilled(this.value);
    36. resolvePromise(promise2, x, resolve, reject);
    37. } catch (e) {
    38. reject(e);
    39. }
    40. }, 0);
    41. };
    42. if (this.state === 'rejected') {
    43. setTimeout(() => {
    44. try {
    45. let x = onRejected(this.reason);
    46. resolvePromise(promise2, x, resolve, reject);
    47. } catch (e) {
    48. reject(e);
    49. }
    50. }, 0);
    51. };
    52. if (this.state === 'pending') {
    53. this.onResolvedCallbacks.push(() => {
    54. setTimeout(() => {
    55. try {
    56. let x = onFulfilled(this.value);
    57. resolvePromise(promise2, x, resolve, reject);
    58. } catch (e) {
    59. reject(e);
    60. }
    61. }, 0);
    62. });
    63. this.onRejectedCallbacks.push(() => {
    64. setTimeout(() => {
    65. try {
    66. let x = onRejected(this.reason);
    67. resolvePromise(promise2, x, resolve, reject);
    68. } catch (e) {
    69. reject(e);
    70. }
    71. }, 0)
    72. });
    73. };
    74. });
    75. return promise2;
    76. }
    77. catch(fn) {
    78. return this.then(null, fn);
    79. }
    80. }
    81. function resolvePromise(promise2, x, resolve, reject) {
    82. if (x === promise2) {
    83. return reject(new TypeError('Chaining cycle detected for promise'));
    84. }
    85. let called;
    86. if (x != null && (typeof x === 'object' || typeof x === 'function')) {
    87. try {
    88. let then = x.then;
    89. if (typeof then === 'function') {
    90. then.call(x, y => {
    91. if (called) return;
    92. called = true;
    93. resolvePromise(promise2, y, resolve, reject);
    94. }, err => {
    95. if (called) return;
    96. called = true;
    97. reject(err);
    98. })
    99. } else {
    100. resolve(x);
    101. }
    102. } catch (e) {
    103. if (called) return;
    104. called = true;
    105. reject(e);
    106. }
    107. } else {
    108. resolve(x);
    109. }
    110. }
    111. //resolve方法
    112. Promise.resolve = function (val) {
    113. return new Promise((resolve, reject) => {
    114. resolve(val)
    115. });
    116. }
    117. //reject方法
    118. Promise.reject = function (val) {
    119. return new Promise((resolve, reject) => {
    120. reject(val)
    121. });
    122. }
    123. //race方法
    124. Promise.race = function (promises) {
    125. return new Promise((resolve, reject) => {
    126. for (let i = 0; i < promises.length; i++) {
    127. promises[i].then(resolve, reject)
    128. };
    129. })
    130. }
    131. //all方法(获取所有的promise,都执行then,把结果放到数组,一起返回)
    132. Promise.all = function (promises) {
    133. let arr = [];
    134. let i = 0;
    135. function processData(index, data) {
    136. arr[index] = data;
    137. i++;
    138. if (i == promises.length) {
    139. resolve(arr);
    140. };
    141. };
    142. return new Promise((resolve, reject) => {
    143. for (let i = 0; i < promises.length; i++) {
    144. promises[i].then(data => {
    145. processData(i, data);
    146. }, reject);
    147. };
    148. });
    149. }
    150. let p1 = new Promise((resolve, reject) => {
    151. setTimeout(() => {
    152. resolve('success')
    153. }, 5000)
    154. })
    155. p1.then((data) => { return data }).then((data) => {
    156. return new Promise((resolve) => resolve(data))
    157. }).then((data) => {
    158. console.log(data, 'end'); // success end
    159. })