Procházet zdrojové kódy

done some fixes for last exercise

unknown před 3 roky
rodič
revize
8cdee0255d
2 změnil soubory, kde provedl 29 přidání a 59 odebrání
  1. 1 1
      src/index.html
  2. 28 58
      src/java-script/hw.js

+ 1 - 1
src/index.html

@@ -13,6 +13,6 @@
       <div class="trafficLights__off"></div>
       <div class="trafficLights__off"></div>
     </div>
-    <button id="resolvePromise" class="resolvePromise">Resolve Promise</button>
+    <button id="resolvePromise" class="resolvePromise">Change Light</button>
   </body>
 </html>

+ 28 - 58
src/java-script/hw.js

@@ -1,5 +1,4 @@
 'use strict';
-
 // Async/Await Homework 2
 
 // Светофор
@@ -54,17 +53,16 @@ async function trafficLightUpdated() {
     await delayUpdated(9000, green);
   }
 }
-
 // trafficLightUpdated();
 
 //domEventPromise + Stage 2 + Stage 3
-// i've done with my way, and do not see any sens in promise race because in this case!!!
-// can't stop promise countDown after btn was clicked and is does count to the end
-const domEventPromise = (btn, type, ms, el) =>
+// i've done with my way, and do not see any sens in promise.race([delayPromise,domEventPromise]) because in this case
+// will not stop execution promise "countDown" after btn was clicked and is does count to the end ,by this reason
+// i decided insert all logic into onePromise "handleTrafficPromise"
+const handleTrafficPromise = (btn, type, ms, el) =>
   new Promise((resolve, _reject) => {
     let n = Math.round(ms / 1000);
     const intervalID = setInterval(handleInterval, 1000);
-
     function handleInterval() {
       if (n === 0) {
         el.textContent = '';
@@ -74,7 +72,6 @@ const domEventPromise = (btn, type, ms, el) =>
       el.textContent = n;
       n -= 1;
     }
-
     const handleResolve = async e => {
       btn.style.backgroundColor = 'grey';
       btn.setAttribute('disabled', '');
@@ -90,90 +87,63 @@ const domEventPromise = (btn, type, ms, el) =>
 
     btn.addEventListener(type, handleResolve);
   });
-
-const btnResolvePromise = document.getElementById('resolvePromise');
-
+const btn = document.getElementById('resolvePromise');
 async function pedestrianTrafficLight() {
   while (true) {
     green.style.backgroundColor = 'gainsboro';
     red.style.backgroundColor = 'red';
-    await domEventPromise(btnResolvePromise, 'click', 9000, red),
+    await handleTrafficPromise(btn, 'click', 9000, red),
       (red.style.backgroundColor = 'gainsboro');
     yellow.style.backgroundColor = 'yellow';
-    await domEventPromise(btnResolvePromise, 'click', 3000, yellow),
+    await handleTrafficPromise(btn, 'click', 3000, yellow),
       (yellow.style.backgroundColor = 'gainsboro');
     green.style.backgroundColor = 'green';
-    await domEventPromise(btnResolvePromise, 'click', 9000, green);
+    await handleTrafficPromise(btn, 'click', 9000, green);
   }
 }
-
 pedestrianTrafficLight();
 
 //speedTest
-
-const dataLuke = async () =>
-  fetch('https://swapi.py4e.com/api/people/1/')
-    .then(res => res.json())
-    .then(data => data)
-    .catch(() => {});
-
 async function speedTest(getPromise, count, parallel = 1) {
-  async function makeIterable(promise, n, parallel) {
+  async function makeIterable() {
     const arr = [];
-    for (let i = 0; i < n; i++) {
+    for (let i = 0; i < count; i++) {
       const arrInner = [];
-      if (parallel > 1) {
-        for (let j = 0; j < parallel; j++) {
-          arrInner.push(i + j);
-        }
-        arr.push(arrInner);
-      }
-      if (parallel < 2) {
-        arr.push(i);
+      for (let j = 0; j < parallel; j++) {
+        arrInner.push([]);
       }
+      arr.push(arrInner);
     }
     return arr;
   }
-
   const arr = await makeIterable(getPromise, count, parallel);
-  let querySpeed = 0;
-  let queryDuration = 0;
-  let parallelDuration = 0;
-  let parallelSpeed = 0;
-  const startLoop = performance.now();
+  let duration = performance.now();
   await Promise.all(
-    arr.map(async el => {
-      if (typeof el === 'object') {
-        return await Promise.all(el.map(async () => await getPromise()));
-      } else {
-        return await getPromise();
-      }
-    }),
-  ).then(data => console.log(data, 'data'));
-
-  const duration = performance.now() - startLoop;
-  querySpeed = duration / count;
-  queryDuration = duration / count;
-  parallelDuration = duration / count;
-  parallelSpeed = duration / (count * parallel);
-
+    arr.map(
+      async el => await Promise.all(el.map(async () => await getPromise())),
+    ),
+  );
+  duration = performance.now() - duration;
   return {
     duration,
-    querySpeed,
-    queryDuration,
-    parallelSpeed,
-    parallelDuration,
+    querySpeed: count / duration,
+    queryDuration: duration / count,
+    parallelSpeed: (count * parallel) / (duration / count),
+    parallelDuration: duration / (count * parallel),
   };
 }
 
 speedTest(() => delay(10000), 10, 10).then(result =>
   console.log(result, 'result delay(10000)'),
 );
-
 speedTest(() => delay(1000), 10, 1).then(result =>
   console.log(result, 'result delay(1000)'),
 );
-
+const dataLuke = async () =>
+  fetch('https://swapi.py4e.com/api/people/1/')
+    .then(res => res.json())
+    .then(data => data)
+    .catch(() => {});
 speedTest(() => dataLuke(), 20, 3).then(result =>
   console.log(result, 'result dataLuke'),
 );