1. "use strict"
    2. /* 工具类方法 */
    3. var def = function def(obj, key, value) {
    4. Object.defineProperty(obj, key, {
    5. value,
    6. writable: true,
    7. configurable: true,
    8. enumerable: false
    9. })
    10. }
    11. /* Promise核心代码 */
    12. function Promise(executor) {
    13. var self = this
    14. if (!(self instanceof Promise)) throw new TypeError("Promise constructor cannot be invoked without 'new'")
    15. if (typeof executor !== "function") throw new TypeError("Promise resolver is not a function")
    16. // 给实例设置私有属性「状态 & 值」
    17. def(self, 'state', 'pending')
    18. def(self, 'result', undefined)
    19. def(self, 'onfulfilledCallback', [])
    20. def(self, 'onrejectedCallback', [])
    21. // 修改实例状态和值的方法
    22. var change = function change(state, result) {
    23. if (self.state !== "pending") return
    24. self.state = state
    25. self.result = result
    26. // 状态修改完毕,要把之前存储的onfulfilled/onrejected执行
    27. var callbacks = state === 'fulfilled' ? self.onfulfilledCallback : self.onrejectedCallback
    28. setTimeout(function () {
    29. callbacks.forEach(function (callback) {
    30. callback(self.result)
    31. })
    32. })
    33. }
    34. // 立即执行 executor 函数
    35. try {
    36. executor(
    37. function resolve(value) {
    38. change('fulfilled', value)
    39. },
    40. function reject(reason) {
    41. change('rejected', reason)
    42. }
    43. )
    44. } catch (err) {
    45. // 如果 executor 函数执行报错了,则修改实例状态是失败,值是报错的原因
    46. change('rejected', err)
    47. }
    48. }
    49. /* Promise.prototype */
    50. var proto = Promise.prototype
    51. def(proto, "then", function then(onfulfilled, onrejected) {
    52. var self = this
    53. if (!(self instanceof Promise)) throw new TypeError("Method Promise.prototype.then called on incompatible receiver #<Promise>")
    54. // 根据现有实例的状态,做不同的处理
    55. switch (self.state) {
    56. case 'fulfilled':
    57. setTimeout(function () {
    58. onfulfilled(self.result)
    59. })
    60. break
    61. case 'rejected':
    62. setTimeout(function () {
    63. onrejected(self.result)
    64. })
    65. break
    66. default:
    67. self.onfulfilledCallback.push(onfulfilled)
    68. self.onrejectedCallback.push(onrejected)
    69. }
    70. })
    71. /* static Promise */
    72. def(Promise, "all", function all(promises) {
    73. // 确保promises是一个数组「内置all方法:要求promises必须具备迭代器规范」
    74. if (!Array.isArray(promises)) throw new TypeError("promises is not a array")
    75. // 返回一个总的实例
    76. return new Promise(function (resolve, reject) {
    77. var count = 0,
    78. len = promises.length,
    79. values = []
    80. // 迭代promises集合中的每一项,根据每一项的状态和值,来决定总实例的状态和值
    81. for (var i = 0; i < len; i++) {
    82. (function (i) {
    83. var promise = promises[i]
    84. // 如果循环这一项不是实例,则把其变为状态是成功,值是本身的实例
    85. if (!(promise instanceof Promise)) promise = Promise.resolve(promise)
    86. promise
    87. .then(function (value) {
    88. // 只要有一项是成功的,则计数器累加
    89. count++
    90. values[i] = value
    91. // 等待所有项都成功,总实例才是成功的
    92. if (count >= len) resolve(values)
    93. })
    94. .catch(function (reason) {
    95. // 只要有一项是失败的,则总实例就是失败的,值就是失败项的值
    96. reject(reason)
    97. })
    98. })(i);
    99. }
    100. })
    101. })

    J37H$IAVILJ7R7[SU56KY]5_tmb.jpg![P]RRD70)WCV]Y4%7663EI4.jpg