finally

time 1m

image.png

  1. finally(cb) {
  2. return this.then(value => {
  3. // cb();
  4. /*返回promise*/
  5. return Promise.resolve(cb()).then(() => value);
  6. }, reason => {
  7. // cb();
  8. return Promise.resolve(cb()).then(() => {
  9. throw reason;
  10. });
  11. })
  12. }
  1. /**
  2. * 不管成功失败都会走的函数
  3. * @param cb 回调函数,变量值都可以,不分resolve、reject,
  4. * 因为它俩必有一个不会执行,而finally里面的内容必然执行
  5. * @returns {MyPromise} 返回promise,里面有value、reason数据
  6. */
  7. finally(cb) {
  8. /*因为要拿到上一次的结果,所以this.then*/
  9. console.log(163,cb);
  10. return this.then(value => {
  11. /*这里需要等待cb的运行,通过then等待,运行then时会有异步处理
  12. * 等待运行,观察者模式*/
  13. // cb();
  14. /*MyPromise.resolve(cb())得到cb的返回值,在这里是promise,但需要里面的value,promise对象
  15. * 里面的数据,所以通过then取得数据*/
  16. console.log(171,value);
  17. /*这里面返回了一个promise对象,里面有return value,有value的数据,
  18. * 返回value交给之后的then方法,外界的then方法,链式调用的then方法*/
  19. return MyPromise.resolve(cb()).then(() => value);
  20. }, reason => {
  21. // cb();
  22. console.log(176,reason);
  23. return MyPromise.resolve(cb()).then(() => {
  24. throw reason;
  25. });
  26. })
  27. }

image.png

all

time 19m56s
time 40m

  1. /**
  2. *
  3. * @param promises 这是一个数组,里面分promise、一般数据不同的处理方式
  4. * @returns {MyPromise}
  5. */
  6. static all(promises) {
  7. return new MyPromise((resolve, reject) => {
  8. let arr = [];
  9. let index = 0;
  10. const processData = (key, value) => {
  11. arr[key] = value;
  12. /*当值相等的时候,证明都放进去了,并且没有错误,这时才能resolve(数组)*/
  13. // if (arr.length === promises.length) {
  14. /*涉及到异步操作时,用push、下标等方法是不靠谱的,如果想保证下标的话,
  15. * 需要用计步器的方法,的方式,新建index变量从0开始记步,循环一遍就加一*/
  16. if (++index === promises.length) {
  17. resolve(arr);
  18. }
  19. /* if (arr.length === promises.length) {
  20. resolve(arr);
  21. }
  22. */
  23. }
  24. /*需要拿到所有promise的结果,再对结果进行判断*/
  25. for (let i = 0; i < promises.length; i++) {
  26. let res = promises[i];
  27. if (isPromsie(res)) {
  28. res.then(data => {
  29. /*需要放入一个数组中*/
  30. // arr.push(data)
  31. /*因为数组内的promise对象之后才会被放进去,所以需要用下标放*/
  32. // arr[i] = data;
  33. /*每放入一次,都需要判断执行一次*/
  34. processData(i, data);
  35. }, err => {
  36. reject(err);
  37. })
  38. } else {
  39. // arr[i] = data;
  40. processData(i, res);
  41. }
  42. }
  43. })
  44. }

race

time 51m

  1. static race(promises) {
  2. return new MyPromise((resolve, reject) => {
  3. for (let i = 0; i < promises.length; i++) {
  4. let res = promises[i];
  5. if (isPromsie(res)) {
  6. /* res.then((data) => {
  7. resolve(data)
  8. }, err => {
  9. reject(err);
  10. })*/
  11. res.then(resolve, reject);
  12. } else {
  13. resolve(res);
  14. }
  15. }
  16. })
  17. }

总结

time 54m

  1. const isFunction = (fn) => typeof fn === 'function';
  2. const isPromsie = (value) => typeof value.then === "function";
  3. class MyPromise {
  4. constructor(executor) {
  5. this.state = 'pending';
  6. this.value = undefined;
  7. this.reason = undefined;
  8. this.onFullFilledCallbacks = [];
  9. this.onRejectedCallbacks = [];
  10. let resolve = (value) => {
  11. if (this.state === 'pending') {
  12. /*time 43m*/
  13. if (isPromsie(value)) {
  14. value.then(data => resolve(data), err => reject(err));
  15. } else {
  16. this.state = 'fullFilled';
  17. this.value = value;
  18. // this.onFullFilledCallbacks.forEach(fn => fn(this.value));
  19. this.onFullFilledCallbacks.forEach(fn => fn());
  20. }
  21. }
  22. }
  23. let reject = (reason) => {
  24. this.state = 'rejected';
  25. this.reason = reason;
  26. // this.onRejectedCallbacks.forEach(fn => fn(this.value));
  27. this.onRejectedCallbacks.forEach(fn => fn());
  28. }
  29. // executor(resolve, reject);
  30. try {
  31. executor(resolve, reject);
  32. } catch (err) {
  33. reject(err)
  34. }
  35. }
  36. then(onFullFilled, onRejected) {
  37. onFullFilled = isFunction(onFullFilled) ? onFullFilled : data => data;
  38. onRejected = isFunction(onRejected) ? onRejected : err => {
  39. throw err;
  40. };
  41. /*因为判断是同步执行的*/
  42. const p2 = new MyPromise((resolve, reject) => {
  43. let x;
  44. if (this.state === 'fullFilled') {
  45. /*让resolvePromise(p2, x, resolve, reject);之后再运行,如果p2声明赋值时*/
  46. setTimeout(() => {
  47. try {
  48. x = onFullFilled(this.value);
  49. // console.log(172, x);
  50. resolvePromise(p2, x, resolve, reject);
  51. } catch (err) {
  52. reject(err)
  53. }
  54. }, 0)
  55. /* x = onFullFilled(this.value);
  56. // console.log('165:'+x)
  57. // resolve(x);
  58. resolvePromise(p2, x, resolve, reject)*/
  59. }
  60. if (this.state === 'rejected') {
  61. setTimeout(() => {
  62. try {
  63. x = onRejected(this.reason);
  64. resolvePromise(p2, x, resolve, reject);
  65. } catch (err) {
  66. reject(err)
  67. }
  68. }, 0)
  69. }
  70. if (this.state === 'pending') {
  71. /* this.onFullFilledCallbacks.push(onFullFilled);
  72. this.onRejectedCallbacks.push(onRejected);*/
  73. this.onFullFilledCallbacks.push(() => {
  74. setTimeout(() => {
  75. try {
  76. x = onFullFilled(this.value);
  77. resolvePromise(p2, x, resolve, reject);
  78. } catch (err) {
  79. reject(err)
  80. }
  81. }, 0)
  82. });
  83. this.onRejectedCallbacks.push(() => {
  84. setTimeout(() => {
  85. try {
  86. x = onRejected(this.reason);
  87. resolvePromise(p2, x, resolve, reject);
  88. } catch (err) {
  89. reject(err)
  90. }
  91. }, 0)
  92. /* x = onRejected(this.reason);
  93. // resolve(x);
  94. resolvePromise(p2, x, resolve, reject);*/
  95. });
  96. }
  97. })
  98. return p2;
  99. }
  100. catch(onRejected) {
  101. return this.then(null, onRejected);
  102. }
  103. /**
  104. *
  105. * @param value
  106. */
  107. static resolve(value) {
  108. /* if (isPromsie(value)) {
  109. // return value;
  110. return value.then((y) => {
  111. });
  112. } else {
  113. return new MyPromise((resolve, reject) => {
  114. resolve(value)
  115. })
  116. }*/
  117. return new MyPromise((resolve, reject) => {
  118. resolve(value)
  119. })
  120. }
  121. finally(cb) {
  122. return this.then(value => {
  123. // cb();
  124. /*返回promise*/
  125. return Promise.resolve(cb()).then(() => value);
  126. }, reason => {
  127. // cb();
  128. return Promise.resolve(cb()).then(() => {
  129. throw reason;
  130. });
  131. })
  132. }
  133. static all(promises) {
  134. return new MyPromise((resolve, reject) => {
  135. let arr = [];
  136. let index = 0;
  137. const processData = (key, value) => {
  138. arr[key] = value;
  139. if (++index === promises.length) {
  140. resolve(arr);
  141. }
  142. /* if (arr.length === promises.length) {
  143. resolve(arr);
  144. }
  145. */
  146. }
  147. for (let i = 0; i < promises.length; i++) {
  148. let res = promises[i];
  149. if (isPromsie(res)) {
  150. res.then(data => {
  151. // arr[i] = data;
  152. processData(i, data);
  153. }, err => {
  154. reject(err);
  155. })
  156. } else {
  157. // arr[i] = data;
  158. processData(i, res);
  159. }
  160. }
  161. })
  162. }
  163. static race(promises) {
  164. return new MyPromise((resolve, reject) => {
  165. for (let i = 0; i < promises.length; i++) {
  166. let res = promises[i];
  167. if (isPromsie(res)) {
  168. /* res.then((data) => {
  169. resolve(data)
  170. }, err => {
  171. reject(err);
  172. })*/
  173. res.then(resolve, reject);
  174. } else {
  175. resolve(res);
  176. }
  177. }
  178. })
  179. }
  180. }
  181. function resolvePromise(p2, x, resolve, reject) {
  182. // console.log(192, p2, x, resolve, reject);
  183. // console.log(209,a);
  184. let called;
  185. if (p2 === x) {
  186. reject(new TypeError('typeErr'));
  187. }
  188. /*x可以是个方法,再return出去一个值*/
  189. if ((typeof x === 'object' && x != null) || x === 'function') {
  190. try {
  191. /*thenable对象,这是处理return new Promise的情况*/
  192. let then = x.then;
  193. if (typeof then === 'function') {
  194. /* then.call(x是让then需要上下文,让then的上下文,变成x,变成
  195. * 返回的promise,相当于返回的promise.then*/
  196. then.call(x, y => {
  197. /*x.then值是一样的*/
  198. // x.then(y => {
  199. /*这时then独立调用指向window*/
  200. // then(y=>{
  201. if (called) return;
  202. called = true;
  203. console.log(250, y);
  204. resolve(y);
  205. }, r => {
  206. if (called) return;
  207. called = true;
  208. console.log(252, r);
  209. reject(r);
  210. })
  211. } else {
  212. if (called) return;
  213. called = true;
  214. resolve(x);
  215. }
  216. } catch (err) {
  217. if (called) return;
  218. called = true;
  219. reject(err);
  220. }
  221. } else {
  222. /*这里不需要锁了*/
  223. resolve(x);
  224. }
  225. }
  226. /*/!*换成mypromise*!/
  227. // const p1 = new Promise((resolve, reject) => {
  228. const p1 = new MyPromise((resolve, reject) => {
  229. // setTimeout(() => {
  230. resolve(1);
  231. // reject(1);
  232. // }, 1000)
  233. })
  234. const p2 = p1.then((res) => {
  235. // console.log(res);
  236. // return res + 1;
  237. /!*返回值是一个Promise,想要拿到里面的reject,怎么拿到,需要通过得到它xx,
  238. * xx.then的方式得到10*!/
  239. return new MyPromise((resolve, reject) => {
  240. reject(10);
  241. })
  242. }, err => {
  243. // console.log(err)
  244. return err + 2;
  245. })
  246. p2.then(res => {
  247. console.log(266, res, 'success');
  248. }, err => {
  249. console.log(268, err, 'error');
  250. })*/
  251. const p1 = new MyPromise((resolve, reject) => {
  252. setTimeout(() => {
  253. reject('1000');
  254. }, 3000)
  255. })
  256. MyPromise.resolve(p1)
  257. .then(res => console.log(res, 'success')
  258. , err => console.log(err, 'fail')
  259. )
  1. const isFunction = (fn) => typeof fn === 'function';
  2. /**
  3. * 判断值是否是promise对象,thanable对象
  4. * @param value 参数,在这里是回调函数,比如promise.resolve的参数,可能是函数,
  5. * 可能是promise对象
  6. * @returns {boolean} 是否是promise对象,thenable对象
  7. */
  8. const isPromsie = (value) => typeof value.then === "function";
  9. class MyPromise {
  10. constructor(executor) {
  11. this.state = 'pending';
  12. this.value = undefined;
  13. this.reason = undefined;
  14. this.onFullFilledCallbacks = [];
  15. this.onRejectedCallbacks = [];
  16. let resolve = (value) => {
  17. if (this.state === 'pending') {
  18. /*time 43m*/
  19. if (isPromsie(value)) {
  20. /*递归调用,继续resolve,如果是reject走reject
  21. * value是promise,但最终还是得resolve或reject,所以得用then
  22. * 运行,为了运行resolve、或者reject,通过then取出promise对象value
  23. * 中的值,进行resolve或者reject,reject是不能运行一个promise对象的*/
  24. value.then(data => resolve(data), err => reject(err));
  25. } else {
  26. this.state = 'fullFilled';
  27. this.value = value;
  28. // this.onFullFilledCallbacks.forEach(fn => fn(this.value));
  29. this.onFullFilledCallbacks.forEach(fn => fn());
  30. }
  31. }
  32. }
  33. let reject = (reason) => {
  34. this.state = 'rejected';
  35. this.reason = reason;
  36. // this.onRejectedCallbacks.forEach(fn => fn(this.value));
  37. this.onRejectedCallbacks.forEach(fn => fn());
  38. }
  39. // executor(resolve, reject);
  40. try {
  41. executor(resolve, reject);
  42. } catch (err) {
  43. reject(err)
  44. }
  45. }
  46. then(onFullFilled, onRejected) {
  47. onFullFilled = isFunction(onFullFilled) ? onFullFilled : data => data;
  48. onRejected = isFunction(onRejected) ? onRejected : err => {
  49. throw err;
  50. };
  51. /*因为判断是同步执行的*/
  52. const p2 = new MyPromise((resolve, reject) => {
  53. let x;
  54. if (this.state === 'fullFilled') {
  55. /*让resolvePromise(p2, x, resolve, reject);之后再运行,如果p2声明赋值时*/
  56. setTimeout(() => {
  57. try {
  58. x = onFullFilled(this.value);
  59. // console.log(172, x);
  60. resolvePromise(p2, x, resolve, reject);
  61. } catch (err) {
  62. reject(err)
  63. }
  64. }, 0)
  65. /* x = onFullFilled(this.value);
  66. // console.log('165:'+x)
  67. // resolve(x);
  68. resolvePromise(p2, x, resolve, reject)*/
  69. }
  70. if (this.state === 'rejected') {
  71. setTimeout(() => {
  72. try {
  73. x = onRejected(this.reason);
  74. resolvePromise(p2, x, resolve, reject);
  75. } catch (err) {
  76. reject(err)
  77. }
  78. }, 0)
  79. }
  80. if (this.state === 'pending') {
  81. /* this.onFullFilledCallbacks.push(onFullFilled);
  82. this.onRejectedCallbacks.push(onRejected);*/
  83. this.onFullFilledCallbacks.push(() => {
  84. setTimeout(() => {
  85. try {
  86. x = onFullFilled(this.value);
  87. resolvePromise(p2, x, resolve, reject);
  88. } catch (err) {
  89. reject(err)
  90. }
  91. }, 0)
  92. });
  93. this.onRejectedCallbacks.push(() => {
  94. setTimeout(() => {
  95. try {
  96. x = onRejected(this.reason);
  97. resolvePromise(p2, x, resolve, reject);
  98. } catch (err) {
  99. reject(err)
  100. }
  101. }, 0)
  102. /* x = onRejected(this.reason);
  103. // resolve(x);
  104. resolvePromise(p2, x, resolve, reject);*/
  105. });
  106. }
  107. })
  108. return p2;
  109. }
  110. catch(onRejected) {
  111. return this.then(null, onRejected);
  112. }
  113. /**
  114. *
  115. * @param value
  116. */
  117. static resolve(value) {
  118. /* if (isPromsie(value)) {
  119. return value;
  120. } else {
  121. return new MyPromise((resolve, reject) => {
  122. resolve(value)
  123. })
  124. }*/
  125. return new MyPromise((resolve, reject) => {
  126. resolve(value)
  127. })
  128. }
  129. /**
  130. * 不管成功失败都会走的函数
  131. * @param cb 回调函数,变量值都可以,不分resolve、reject,
  132. * 因为它俩必有一个不会执行,而finally里面的内容必然执行
  133. * @returns {MyPromise} 返回promise,里面有value、reason数据
  134. */
  135. finally(cb) {
  136. /*因为要拿到上一次的结果,所以this.then*/
  137. console.log(163, cb);
  138. return this.then(value => {
  139. /*这里需要等待cb的运行,通过then等待,运行then时会有异步处理
  140. * 等待运行,观察者模式*/
  141. // cb();
  142. /*MyPromise.resolve(cb())得到cb的返回值,在这里是promise,但需要里面的value,promise对象
  143. * 里面的数据,所以通过then取得数据*/
  144. console.log(171, value);
  145. /*这里面返回了一个promise对象,里面有return value,有value的数据,
  146. * 返回value交给之后的then方法,外界的then方法,链式调用的then方法*/
  147. return MyPromise.resolve(cb()).then(() => value);
  148. }, reason => {
  149. // cb();
  150. console.log(176, reason);
  151. return MyPromise.resolve(cb()).then(() => {
  152. throw reason;
  153. });
  154. })
  155. }
  156. /**
  157. *
  158. * @param promises 这是一个数组,里面分promise、一般数据不同的处理方式
  159. * @returns {MyPromise}
  160. */
  161. static all(promises) {
  162. return new MyPromise((resolve, reject) => {
  163. let arr = [];
  164. let index = 0;
  165. const processData = (key, value) => {
  166. arr[key] = value;
  167. /*当值相等的时候,证明都放进去了,并且没有错误,这时才能resolve(数组)*/
  168. // if (arr.length === promises.length) {
  169. /*涉及到异步操作时,用push、下标等方法是不靠谱的,如果想保证下标的话,
  170. * 需要用计步器的方法,的方式,新建index变量从0开始记步,循环一遍就加一*/
  171. if (++index === promises.length) {
  172. resolve(arr);
  173. }
  174. /* if (arr.length === promises.length) {
  175. resolve(arr);
  176. }
  177. */
  178. }
  179. /*需要拿到所有promise的结果,再对结果进行判断*/
  180. for (let i = 0; i < promises.length; i++) {
  181. let res = promises[i];
  182. if (isPromsie(res)) {
  183. res.then(data => {
  184. /*需要放入一个数组中*/
  185. // arr.push(data)
  186. /*因为数组内的promise对象之后才会被放进去,所以需要用下标放*/
  187. // arr[i] = data;
  188. /*每放入一次,都需要判断执行一次*/
  189. processData(i, data);
  190. }, err => {
  191. reject(err);
  192. })
  193. } else {
  194. // arr[i] = data;
  195. processData(i, res);
  196. }
  197. }
  198. })
  199. }
  200. static race(promises) {
  201. return new MyPromise((resolve, reject) => {
  202. for (let i = 0; i < promises.length; i++) {
  203. let res = promises[i];
  204. if (isPromsie(res)) {
  205. /* res.then((data) => {
  206. resolve(data)
  207. }, err => {
  208. reject(err);
  209. })*/
  210. res.then(resolve, reject);
  211. } else {
  212. resolve(res);
  213. }
  214. }
  215. })
  216. }
  217. }
  218. function resolvePromise(p2, x, resolve, reject) {
  219. // console.log(192, p2, x, resolve, reject);
  220. // console.log(209,a);
  221. let called;
  222. if (p2 === x) {
  223. reject(new TypeError('typeErr'));
  224. }
  225. /*x可以是个方法,再return出去一个值*/
  226. if ((typeof x === 'object' && x != null) || x === 'function') {
  227. try {
  228. /*thenable对象,这是处理return new Promise的情况*/
  229. let then = x.then;
  230. if (typeof then === 'function') {
  231. /* then.call(x是让then需要上下文,让then的上下文,变成x,变成
  232. * 返回的promise,相当于返回的promise.then*/
  233. then.call(x, y => {
  234. /*x.then值是一样的*/
  235. // x.then(y => {
  236. /*这时then独立调用指向window*/
  237. // then(y=>{
  238. if (called) return;
  239. called = true;
  240. // console.log(250, y);
  241. resolve(y);
  242. }, r => {
  243. if (called) return;
  244. called = true;
  245. // console.log(252, r);
  246. reject(r);
  247. })
  248. } else {
  249. if (called) return;
  250. called = true;
  251. resolve(x);
  252. }
  253. } catch (err) {
  254. if (called) return;
  255. called = true;
  256. reject(err);
  257. }
  258. } else {
  259. /*这里不需要锁了*/
  260. resolve(x);
  261. }
  262. }
  263. /*/!*换成mypromise*!/
  264. // const p1 = new Promise((resolve, reject) => {
  265. const p1 = new MyPromise((resolve, reject) => {
  266. // setTimeout(() => {
  267. resolve(1);
  268. // reject(1);
  269. // }, 1000)
  270. })
  271. const p2 = p1.then((res) => {
  272. // console.log(res);
  273. // return res + 1;
  274. /!*返回值是一个Promise,想要拿到里面的reject,怎么拿到,需要通过得到它xx,
  275. * xx.then的方式得到10*!/
  276. return new MyPromise((resolve, reject) => {
  277. reject(10);
  278. })
  279. }, err => {
  280. // console.log(err)
  281. return err + 2;
  282. })
  283. p2.then(res => {
  284. console.log(266, res, 'success');
  285. }, err => {
  286. console.log(268, err, 'error');
  287. })*/
  288. /*const p1 = new MyPromise((resolve, reject) => {
  289. setTimeout(() => {
  290. reject('1000');
  291. }, 3000)
  292. })*/
  293. const p1 = new MyPromise((resolve, reject) => {
  294. resolve('1000');
  295. })
  296. /*MyPromise.resolve(p1)
  297. .then(res => console.log(res, 'success')
  298. , err => console.log(err, 'fail')
  299. )*/
  300. MyPromise.resolve(p1).finally((res) => {
  301. return new MyPromise((resolve, reject) => {
  302. setTimeout(() => {
  303. resolve(110);
  304. }, 3000)
  305. })
  306. })
  307. .then(res => console.log(res, 'success')
  308. , err => {
  309. console.log(err, 'fail')
  310. });