1. /**
    2. * Promise构造函数
    3. * executor: 内部同步执行的函数(resolve, reject) => {}
    4. * new Promise((resolve, reject) => {
    5. * setTimeout(() => { resolve(42); }, 1000);
    6. * })
    7. */
    8. function Promise2(executor) {
    9. // 初始状态
    10. this.PromiseState = "pending";
    11. // 初始结果
    12. this.PromiseResult = null;
    13. // 保存回调函数
    14. this.callbacks = [];
    15. const self = this;
    16. // resolve函数
    17. function resolve(value) {
    18. // 判断状态,让Promise对象只能修改一次
    19. if(self.PromiseState !== "pending") return;
    20. // 修改状态
    21. self.PromiseState = "fulfilled";
    22. // 修改结果
    23. self.PromiseResult = value;
    24. // 调用回调函数
    25. setTimeout(() => {
    26. self.callbacks.forEach(element => {
    27. element.onResolved(value);
    28. });
    29. });
    30. }
    31. // reject函数
    32. function reject(reason) {
    33. // 判断状态,让Promise对象只能修改一次
    34. if(self.PromiseState !== "pending") return;
    35. // 修改状态
    36. self.PromiseState = "rejected";
    37. // 修改结果
    38. self.PromiseResult = reason;
    39. // 调用回调函数
    40. setTimeout(() => {
    41. self.callbacks.forEach(element => {
    42. element.onRejected(reason);
    43. });
    44. });
    45. }
    46. // 同步调用执行器函数
    47. try {
    48. executor(resolve, reject);
    49. } catch (error) {
    50. reject(error);
    51. }
    52. }
    53. Promise2.prototype.then = function(onResolved, onRejected) {
    54. if(typeof onResolved !== "function") {
    55. onResolved = value => value;
    56. }
    57. if(typeof onRejected !== "function") {
    58. onRejected = reason => {
    59. throw new Error(reason);
    60. }
    61. }
    62. // 固定返回一个Promise对象,这里我们称为newPro对象,供下一环节使用
    63. // 注意这里是箭头函数,所以里面this指向oldPro对象(最初创建的Promise对象,称为oldPro)
    64. return new Promise2((resolve, reject) => {
    65. function handle(callback) {
    66. try {
    67. let result = callback(this.PromiseResult);
    68. // 当result为Promise对象,这里我们称为resultPro,这个resultPro是外部自定义的
    69. if(result instanceof Promise) {
    70. // then方法将v函数压入resultPro对象的callbacks数组里,
    71. // 当resultPro对象执行resolve函数时触发callbacks数组执行v函数,
    72. // 执行v函数触发newPro对象的resolve函数,继而触发下一环节Promise对象执行onResolved函数
    73. result.then(
    74. // 该onResolved函数这里我们称为v函数,参数值为resultPro对象执行resolve函数时传入的值
    75. v => {
    76. // resolve为Promise构造函数内置函数
    77. resolve(v);
    78. },
    79. r => {
    80. reject(r);
    81. }
    82. );
    83. } else {
    84. // 触发newPro对象的resolve函数,并将result值向下一环节Promise对象的onResolved函数传递
    85. resolve(result);
    86. }
    87. } catch (error) {
    88. reject(error);
    89. }
    90. }
    91. if(this.PromiseState === "pending") {
    92. // 将then方法的onResolved函数压入oldPro对象的callbacks数组,供oldPro执行resolve方法调用
    93. this.callbacks.push({
    94. onResolved: (value) => {
    95. handle.call(this, onResolved);
    96. },
    97. onRejected (reason) {
    98. handle.call(this, onRejected);
    99. }
    100. });
    101. }
    102. if(this.PromiseState === "fulfilled") {
    103. setTimeout(() => {
    104. handle(onResolved);
    105. })
    106. }
    107. if(this.PromiseState === "rejected") {
    108. setTimeout(() => {
    109. handle(onRejected);
    110. })
    111. }
    112. });
    113. }