简单版

手动实现Promise功能

  1. class LikePromise {
  2. constructor(exec) {
  3. this._resolve = null
  4. exec(this.resolve.bind(this), null)
  5. }
  6. resolve(value) {
  7. setTimeout(() => {
  8. this._resolve(value)
  9. }, 0)
  10. }
  11. then(resolveFunc) {
  12. this._resolve = resolveFunc
  13. }
  14. }
  15. var p1 = new LikePromise((resolve) => {
  16. resolve(10)
  17. })
  18. p1.then((data) => {
  19. console.log('--data--', data);
  20. })

综合版

  1. class MockPromise {
  2. constructor(exec) {
  3. this.status = 'pending'
  4. this.value = null
  5. this.resolvedTasks = []
  6. this.rejectedTasks = []
  7. this._resolve = this._resolve.bind(this)
  8. this._reject = this._reject.bind(this)
  9. // catch err 到 then 中的 onRejected 的reason
  10. try {
  11. exec(this._resolve, this._reject)
  12. } catch (error) {
  13. this._reject(error)
  14. }
  15. }
  16. _resolve(value) {
  17. if (this.status === MockPromise.PENDING) {
  18. this.status = MockPromise.FULFILLED
  19. this.value = value
  20. this.resolvedTasks.forEach((fn) => {
  21. fn(value)
  22. })
  23. }
  24. }
  25. _reject(reason) {
  26. if (this.status === MockPromise.PENDING) {
  27. this.status = MockPromise.REJECTED
  28. this.value = reason
  29. this.rejectedTasks.forEach((fn) => {
  30. fn(reason)
  31. })
  32. }
  33. }
  34. then(onFulfilled, onRejected) {
  35. if (typeof onFulfilled !== 'function') {
  36. onFulfilled = (value) => {
  37. return value
  38. }
  39. }
  40. if (typeof onRejected !== 'function') {
  41. onRejected = (resaon) => {
  42. throw resaon
  43. }
  44. }
  45. /**
  46. * 实现链式调用 return 一个promise
  47. * 判断 onFulfilled() 的返回值类型就好了
  48. */
  49. return new MockPromise((resolve, reject) => {
  50. if (this.status === MockPromise.FULFILLED) {
  51. setTimeout(() => {
  52. try {
  53. const res = onFulfilled(this.value)
  54. if (res instanceof MockPromise) {
  55. res.then(value => {
  56. resolve(value)
  57. })
  58. } else {
  59. resolve(res)
  60. }
  61. } catch (error) {
  62. reject(error)
  63. }
  64. })
  65. }
  66. if (this.status === MockPromise.REJECTED) {
  67. setTimeout(() => {
  68. try {
  69. const res = onRejected(this.value)
  70. if (res instanceof MockPromise) {
  71. res.then(value => {
  72. resolve(value)
  73. })
  74. } else {
  75. resolve(res)
  76. }
  77. } catch (error) {
  78. reject(error)
  79. }
  80. })
  81. }
  82. /**解决 resolve() 异步处理
  83. * 比如 setTimeout(() => { resolve(3) }, 1000)
  84. * 这时候 先执行 的是 then 函数,我们可以先把要执行的函数 压入 一个数组中
  85. * 等到执行到 resolve 的时候,再去判断状态,然后遍历数组将数组中的函数执行
  86. */
  87. if (this.status === MockPromise.PENDING) {
  88. // push 的肯定是一个函数
  89. this.resolvedTasks.push((value) => {
  90. setTimeout(() => {
  91. const res = onFulfilled(value)
  92. console.log('res===',res );
  93. MockPromise.handlePromise(res, resolve, reject)
  94. // try {
  95. // const res = onFulfilled(value)
  96. // if (res instanceof MockPromise) {
  97. // res.then(value => {
  98. // resolve(value)
  99. // })
  100. // } else {
  101. // resolve(res)
  102. // }
  103. // } catch (error) {
  104. // reject(error)
  105. // }
  106. })
  107. })
  108. this.rejectedTasks.push((reason) => {
  109. setTimeout(() => {
  110. try {
  111. const res = onRejected(reason)
  112. if (res instanceof MockPromise) {
  113. res.then(value => {
  114. resolve(value)
  115. })
  116. } else {
  117. resolve(res)
  118. }
  119. } catch (error) {
  120. reject(error)
  121. }
  122. })
  123. })
  124. }
  125. })
  126. // if (this.status === MockPromise.FULFILLED) {
  127. // setTimeout(() => {
  128. // onFulfilled(this.value)
  129. // })
  130. // }
  131. // if (this.status === MockPromise.REJECTED) {
  132. // setTimeout(() => {
  133. // onRejected(this.value)
  134. // })
  135. // }
  136. // /**解决 resolve() 异步处理
  137. // * 比如 setTimeout(() => { resolve(3) }, 1000)
  138. // * 这时候 先执行 的是 then 函数,我们可以先把要执行的函数 压入 一个数组中
  139. // * 等到执行到 resolve 的时候,再去判断状态,然后遍历数组将数组中的函数执行
  140. // */
  141. // if (this.status === MockPromise.PENDING) {
  142. // // push 的肯定是一个函数
  143. // this.resolvedTasks.push((value) => {
  144. // setTimeout(() => {
  145. // const res = onFulfilled(value)
  146. // console.log('res---', res)
  147. // })
  148. // })
  149. // this.rejectedTasks.push((reason) => {
  150. // setTimeout(() => {
  151. // onRejected(reason)
  152. // })
  153. // })
  154. // }
  155. }
  156. }
  157. MockPromise.PENDING = 'pending'
  158. MockPromise.FULFILLED = 'fulfilled'
  159. MockPromise.REJECTED = 'rejected'
  160. MockPromise.handlePromise = function (res, resolve, reject) {
  161. try {
  162. if (res instanceof MockPromise) {
  163. res.then(value => {
  164. resolve(value)
  165. })
  166. } else {
  167. resolve(res)
  168. }
  169. } catch (error) {
  170. reject(error)
  171. }
  172. }
  173. const dd = new MockPromise((resolve, reject) => {
  174. console.log('1');
  175. setTimeout(() => {
  176. resolve(3)
  177. }, 200)
  178. }).then((val) => {
  179. console.log('val--', val);
  180. // return 'haha' + val
  181. return new MockPromise((resolve, reject) => {
  182. resolve('新的promise' + val)
  183. })
  184. }, reason => {
  185. console.log('reason--', reason);
  186. })
  187. .then(val => {
  188. console.log('then--', val);
  189. })
  190. console.log('2');