1. /**
    2. * 判断then方法传入的参数是否是方法
    3. * @param value 这里把then的参数传入
    4. * @returns {boolean}
    5. */
    6. const isFunction = (value) => typeof value === 'function';
    7. class MyPromise {
    8. constructor(executor) {
    9. this.state = 'pending';//设置实例对象的state状态,默认是pedding
    10. this.value = undefined;//储存resolve回调函数的value的值,的数据
    11. this.reason = undefined;//储存reject的回调函数的reason的数据
    12. this.onFullFilledCallbacks = [];//储存异步操作成功的回调函数
    13. this.onRejectedCallbacks = [];//储存异步操作失败的回调函数
    14. //设置成功回调函数
    15. let resolve = (value) => {
    16. this.state = 'fullFilled';
    17. this.value = value;
    18. /*当回调函数运行时,运行储存在数组中函数*/
    19. this.onFullFilledCallbacks.forEach(fn => fn());
    20. }
    21. /*失败回调函数*/
    22. let reject = (reason) => {
    23. this.state = 'rejected';
    24. this.reason = reason;
    25. this.onRejectedCallbacks.forEach(fn => fn());
    26. }
    27. // executor(resolve, reject);
    28. /*try catch捕获异常,失败需要走reject*/
    29. try {
    30. executor(resolve, reject);
    31. } catch (err) {
    32. reject(err)
    33. }
    34. }
    35. /**
    36. *then方法
    37. * @param onFullFilled 成功的回调
    38. * @param onRejected 失败的回调
    39. * @returns {MyPromise} 返回值是promise对象
    40. */
    41. then(onFullFilled, onRejected) {
    42. /*判断是否是方法,是方法就返回方法,不是方法,就组装成方法*/
    43. onFullFilled = isFunction(onFullFilled) ? onFullFilled : data => data;
    44. /*是方法返回方法,不是方法抛出错误*/
    45. onRejected = isFunction(onRejected) ? onRejected : err => {
    46. throw err;
    47. };
    48. /**
    49. * 新建新的mypromie,用于返回,这是为了能够可以链式调用
    50. * @type {MyPromise}
    51. */
    52. const p2 = new MyPromise((resolve, reject) => {
    53. let x;
    54. if (this.state === 'fullFilled') {
    55. /*让resolvePromise(p2, x, resolve, reject);之后再运行,因为让resolvePromise调用p2时,p2并没有没有生成*/
    56. setTimeout(() => {
    57. /*当异常时通过reject抛出异常*/
    58. try {
    59. x = onFullFilled(this.value);
    60. // console.log(172, x);
    61. resolvePromise(p2, x, resolve, reject);
    62. } catch (err) {
    63. reject(err)
    64. }
    65. }, 0)
    66. /* x = onFullFilled(this.value);
    67. // console.log('165:'+x)
    68. // resolve(x);
    69. resolvePromise(p2, x, resolve, reject)*/
    70. }
    71. if (this.state === 'rejected') {
    72. setTimeout(() => {
    73. try {
    74. x = onRejected(this.reason);
    75. resolvePromise(p2, x, resolve, reject);
    76. } catch (err) {
    77. reject(err)
    78. }
    79. }, 0)
    80. }
    81. if (this.state === 'pending') {
    82. /* this.onFullFilledCallbacks.push(onFullFilled);
    83. this.onRejectedCallbacks.push(onRejected);*/
    84. /*存入成功的回调函数,观察者模式,先记录需要观察什么东西*/
    85. this.onFullFilledCallbacks.push(() => {
    86. setTimeout(() => {
    87. try {
    88. x = onFullFilled(this.value);
    89. resolvePromise(p2, x, resolve, reject);
    90. } catch (err) {
    91. reject(err)
    92. }
    93. }, 0)
    94. });
    95. this.onRejectedCallbacks.push(() => {
    96. setTimeout(() => {
    97. try {
    98. x = onRejected(this.reason);
    99. resolvePromise(p2, x, resolve, reject);
    100. } catch (err) {
    101. reject(err)
    102. }
    103. }, 0)
    104. /* x = onRejected(this.reason);
    105. // resolve(x);
    106. resolvePromise(p2, x, resolve, reject);*/
    107. });
    108. }
    109. })
    110. return p2;
    111. }
    112. }
    113. /**
    114. * 因为promise规范规定 x不能等于p2
    115. * @param p2 then运行的返回值,promise
    116. * @param x 成功失败函数运行返回的值,回调函数传入实参运行返回的值
    117. * @param resolve 成功回调
    118. * @param reject 失败回调
    119. */
    120. function resolvePromise(p2, x, resolve, reject) {
    121. // console.log(192, p2, x, resolve, reject);
    122. // console.log(209,a);
    123. //为了让回调函数执行一次上一把锁
    124. let called;
    125. if (p2 === x) {
    126. reject(new TypeError('typeErr'));
    127. }
    128. /*x可以是个方法,再return出去一个值*/
    129. if ((typeof x === 'object' && x != null) || x === 'function') {
    130. try {
    131. /*thenable对象,这是处理return new Promise的情况*/
    132. let then = x.then;
    133. if (typeof then === 'function') {
    134. /* then.call(x是让then需要上下文,让then的上下文,变成x,变成
    135. * 返回的promise,相当于返回的promise.then*/
    136. then.call(x, y => {
    137. /*x.then值是一样的*/
    138. // x.then(y => {
    139. /*这时then独立调用指向window*/
    140. // then(y=>{
    141. /*true时,走return直接终止函数运行,终止resolvePromise的运行,不是mypromise的运行*/
    142. if (called) return;
    143. called = true;
    144. console.log(250, y);
    145. resolve(y);
    146. }, r => {
    147. if (called) return;
    148. called = true;
    149. console.log(252, r);
    150. reject(r);
    151. })
    152. } else {
    153. if (called) return;
    154. called = true;
    155. resolve(x);
    156. }
    157. } catch (err) {
    158. if (called) return;
    159. called = true;
    160. reject(err);
    161. }
    162. } else {
    163. /*这里不需要锁了*/
    164. resolve(x);
    165. }
    166. }
    167. /*
    168. /!*换成mypromise*!/
    169. // const p1 = new Promise((resolve, reject) => {
    170. const p1 = new MyPromise((resolve, reject) => {
    171. // setTimeout(() => {
    172. resolve(1);
    173. // reject(1);
    174. // }, 1000)
    175. })
    176. const p2 = p1.then((res) => {
    177. // console.log(res);
    178. // return res + 1;
    179. /!*返回值是一个Promise,想要拿到里面的reject,怎么拿到,需要通过得到它xx,
    180. * xx.then的方式得到10*!/
    181. return new MyPromise((resolve, reject) => {
    182. reject(10);
    183. })
    184. }, err => {
    185. // console.log(err)
    186. return err + 2;
    187. })
    188. p2.then(res => {
    189. console.log(266, res, 'success');
    190. }, err => {
    191. console.log(268, err, 'error');
    192. })
    193. */
    194. /*3 success*/
    195. MyPromise.defer = MyPromise.deferred = function () {
    196. let dfd = {};
    197. dfd.promise = new MyPromise((resolve, reject) => {
    198. dfd.resolve = resolve;
    199. dfd.reject = reject;
    200. })
    201. return dfd;
    202. }
    203. module.exports = MyPromise;