Alyona Brytvina пре 2 година
родитељ
комит
78868d0e50
1 измењених фајлова са 5 додато и 7 уклоњено
  1. 5 7
      HW12/main.js

+ 5 - 7
HW12/main.js

@@ -84,14 +84,13 @@ function runMyFetch() {
 
 function runRace() {
     function myFetch(url) {
-        return Promise.race([new Promise(function (resolve, reject) {
+        return new Promise(function (resolve, reject) {
             const xhr = new XMLHttpRequest();
             console.log(url);
             xhr.open('GET', url);
             xhr.send();
 
             xhr.onload = function () {
-                console.log(xhr.status, xhr.readyState);
                 if (xhr.readyState === 4 && xhr.status === 200) {
                     resolve(JSON.parse(xhr.responseText));
                 }
@@ -100,13 +99,12 @@ function runRace() {
                 reject(`Ошибка ${xhr.status}${xhr.statusText}`);
             };
         })
-        ]);
     }
-    myFetch('https://swapi.dev/api/people/1/')
-        .then(luke => console.log(luke));
-
     let randomSpeed = (Math.random() * 1000).toFixed(1);
-    setTimeout(() => console.log(`я тут через ${randomSpeed} ms`), randomSpeed );
+    let delay = () => setTimeout(() => console.log(`я тут через ${randomSpeed} ms`), randomSpeed );
+
+    Promise.race([myFetch('https://swapi.dev/api/people/1/')
+        .then(luke => console.log(luke)), delay()]);
 }