Guest User

Untitled

a guest
Jul 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. const sleep=(fn,ms)=>new Promise(r=>setTimeout(()=>r(fn()),ms));
  2. const init=Date.now();
  3. const log=(y)=>console.log((Date.now()-init)+' '+y);
  4.  
  5. //7: Awaiting on required values immediately. Very slow.
  6. (async () => {
  7. const x=await sleep(()=>1,100);
  8. const a=await sleep(()=>3,200);
  9. const b=await sleep(()=>4,300);
  10.  
  11. const ax=await sleep(()=>a*x**2,1000);
  12. const bx=await sleep(()=>b*x,500);
  13.  
  14. return ax + bx;
  15. })().then(log);
  16.  
  17. /*20: Initialize promises, then await. This is faster, but ax_p can't run until
  18. b_p resolves. We could await b_p after ax_p because ax_p doesn't need b_p and
  19. we know b_p will take longer to resolve since we can see the timings, but in
  20. real world scenarios, timings are unpredictable.*/
  21. (async () => {
  22. const x_p=sleep(()=>2,100);
  23. const a_p=sleep(()=>3,200);
  24. const b_p=sleep(()=>4,300);
  25.  
  26. const x=await x_p;
  27. const a=await a_p;
  28. const b=await b_p;
  29.  
  30. const ax_p=sleep(()=>a*x**2,1000);
  31. const bx_p=sleep(()=>b*x,500);
  32.  
  33. return await ax_p + await bx_p;
  34. })().then(log);
  35.  
  36. //39: Same as before, but with Promise.all().
  37. (async () => {
  38. const x_p=sleep(()=>3,100);
  39. const a_p=sleep(()=>3,200);
  40. const b_p=sleep(()=>4,300);
  41.  
  42. const [x,a,b]=await Promise.all([x_p,a_p,b_p]);
  43. const [ax,bx]=await Promise.all([
  44. sleep(()=>a*x**2,1000),
  45. sleep(()=>b*x,500)
  46. ]);
  47.  
  48. return ax + bx;
  49. })().then(log);
  50.  
  51. //64: Use nested async/await to only await the values needed for each result.
  52. (async () => {
  53. const x_p=sleep(()=>4,100);
  54. const a_p=sleep(()=>3,200);
  55. const b_p=sleep(()=>4,300);
  56.  
  57. const ax_p=(async ()=>{
  58. const a=await a_p;
  59. const x=await x_p;
  60. return await sleep(()=>a*x**2,1000);
  61. })();
  62. const bx_p=(async ()=>{
  63. const b=await b_p;
  64. const x=await x_p;
  65. return await sleep(()=>b*x,500);
  66. })();
  67.  
  68. return await ax_p + await bx_p;
  69. })().then(log);
  70.  
  71. //95: Same as before, but with Promise.all().
  72. (async () => {
  73. const x_p=sleep(()=>5,100);
  74. const a_p=sleep(()=>3,200);
  75. const b_p=sleep(()=>4,300);
  76.  
  77. const [ax,bx]=await Promise.all([
  78. (async ()=>{
  79. const [a,x]=await Promise.all([a_p,x_p]);
  80. return await sleep(()=>a*x**2,1000);
  81. })(),
  82. (async ()=>{
  83. const [b,x]=await Promise.all([b_p,x_p]);
  84. return await sleep(()=>b*x,500);
  85. })()
  86. ]);
  87.  
  88. return ax + bx;
  89. })().then(log);
  90.  
  91. //132: Same as before, but without any async/await, just raw promises.
  92. (() => {
  93. const x_p=sleep(()=>6,100);
  94. const a_p=sleep(()=>3,200);
  95. const b_p=sleep(()=>4,300);
  96.  
  97. const ax_p=Promise.all([a_p,x_p]).then(([a,x])=>sleep(()=>a*x**2,1000));
  98. const bx_p=Promise.all([b_p,x_p]).then(([b,x])=>sleep(()=>b*x,500));
  99.  
  100. return Promise.all([ax_p,bx_p]).then(([ax,bx])=>ax+bx);
  101. })().then(log);
  102.  
  103. /*
  104. End result:
  105. 1203 132
  106. 1205 64
  107. 1205 95
  108. 1304 20
  109. 1304 39
  110. 2104 7
  111. */
Add Comment
Please, Sign In to add comment