hw.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. // Async/Await Homework 2
  3. // Светофор
  4. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));
  5. const lights = document.getElementById('traffic').children;
  6. const red = lights[0];
  7. const yellow = lights[1];
  8. const green = lights[2];
  9. async function trafficLight() {
  10. while (true) {
  11. green.style.backgroundColor = 'gainsboro';
  12. red.style.backgroundColor = 'red';
  13. await delay(1000);
  14. red.style.backgroundColor = 'gainsboro';
  15. yellow.style.backgroundColor = 'yellow';
  16. await delay(1000);
  17. yellow.style.backgroundColor = 'gainsboro';
  18. green.style.backgroundColor = 'green';
  19. await delay(1000);
  20. }
  21. }
  22. // trafficLight();
  23. // Stage 2
  24. const delayUpdated = (ms, el) =>
  25. new Promise((resolve, _reject) => {
  26. let n = Math.round(ms / 1000);
  27. const intervalID = setInterval(handleInterval, 1000);
  28. function handleInterval() {
  29. if (n === 0) {
  30. el.textContent = '';
  31. clearInterval(intervalID);
  32. return resolve(ms);
  33. }
  34. el.textContent = n;
  35. n -= 1;
  36. }
  37. });
  38. async function trafficLightUpdated() {
  39. while (true) {
  40. green.style.backgroundColor = 'gainsboro';
  41. red.style.backgroundColor = 'red';
  42. await delayUpdated(9000, red);
  43. red.style.backgroundColor = 'gainsboro';
  44. yellow.style.backgroundColor = 'yellow';
  45. await delayUpdated(3000, yellow);
  46. yellow.style.backgroundColor = 'gainsboro';
  47. green.style.backgroundColor = 'green';
  48. await delayUpdated(9000, green);
  49. }
  50. }
  51. // trafficLightUpdated();
  52. //domEventPromise + Stage 2 + Stage 3
  53. // i've done with my way, and do not see any sens in promise.race([delayPromise,domEventPromise]) because in this case
  54. // will not stop execution promise "countDown" after btn was clicked and is does count to the end ,by this reason
  55. // i decided insert all logic into onePromise "handleTrafficPromise"
  56. const handleTrafficPromise = (btn, type, ms, el) =>
  57. new Promise((resolve, _reject) => {
  58. let n = Math.round(ms / 1000);
  59. const intervalID = setInterval(handleInterval, 1000);
  60. function handleInterval() {
  61. if (n === 0) {
  62. el.textContent = '';
  63. clearInterval(intervalID);
  64. return resolve(ms);
  65. }
  66. el.textContent = n;
  67. n -= 1;
  68. }
  69. const handleResolve = async e => {
  70. btn.style.backgroundColor = 'grey';
  71. btn.setAttribute('disabled', '');
  72. el.textContent = '';
  73. clearInterval(intervalID);
  74. resolve(e);
  75. btn.removeEventListener(type, handleResolve);
  76. await setTimeout(() => {
  77. btn.removeAttribute('disabled');
  78. btn.style.backgroundColor = 'green';
  79. }, 2000);
  80. };
  81. btn.addEventListener(type, handleResolve);
  82. });
  83. const btn = document.getElementById('resolvePromise');
  84. async function pedestrianTrafficLight() {
  85. while (true) {
  86. green.style.backgroundColor = 'gainsboro';
  87. red.style.backgroundColor = 'red';
  88. await handleTrafficPromise(btn, 'click', 9000, red),
  89. (red.style.backgroundColor = 'gainsboro');
  90. yellow.style.backgroundColor = 'yellow';
  91. await handleTrafficPromise(btn, 'click', 3000, yellow),
  92. (yellow.style.backgroundColor = 'gainsboro');
  93. green.style.backgroundColor = 'green';
  94. await handleTrafficPromise(btn, 'click', 9000, green);
  95. }
  96. }
  97. pedestrianTrafficLight();
  98. //speedTest
  99. async function speedTest(getPromise, count, parallel = 1) {
  100. async function makeIterable() {
  101. const arr = [];
  102. for (let i = 0; i < count; i++) {
  103. const arrInner = [];
  104. for (let j = 0; j < parallel; j++) {
  105. arrInner.push([]);
  106. }
  107. arr.push(arrInner);
  108. }
  109. return arr;
  110. }
  111. let duration = performance.now();
  112. const arr = await makeIterable();
  113. await Promise.all(
  114. arr.map(
  115. async el => await Promise.all(el.map(async () => await getPromise())),
  116. ),
  117. );
  118. duration = performance.now() - duration;
  119. return {
  120. duration,
  121. querySpeed: count / duration,
  122. queryDuration: duration / count,
  123. parallelSpeed: (count * parallel) / (duration / count),
  124. parallelDuration: duration / (count * parallel),
  125. };
  126. }
  127. speedTest(() => delay(10000), 10, 10).then(result =>
  128. console.log(result, 'result delay(10000)'),
  129. );
  130. speedTest(() => delay(1000), 10, 1).then(result =>
  131. console.log(result, 'result delay(1000)'),
  132. );
  133. const dataLuke = async () =>
  134. fetch('https://swapi.py4e.com/api/people/1/')
  135. .then(res => res.json())
  136. .then(data => data)
  137. .catch(() => {});
  138. speedTest(() => dataLuke(), 20, 3).then(result =>
  139. console.log(result, 'result dataLuke'),
  140. );