myFetch.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. function myfetch(url){
  11. return new Promise(function (resolve, reject){
  12. console.log('panding')
  13. const xhr = new XMLHttpRequest()
  14. xhr.open('GET', url);
  15. xhr.send();
  16. xhr.onload = function() {
  17. if (xhr.status != 200) {
  18. reject("xhr.status != 200")
  19. return
  20. }
  21. resolve(JSON.parse(xhr.response));
  22. }
  23. xhr.onerror = function() {
  24. reject("Error");
  25. }
  26. })
  27. }
  28. let delay2 = function (time) {
  29. return new Promise(function (resolve, reject) {
  30. console.log("delay starsing")
  31. setTimeout(() => resolve("Delay was first"), time);
  32. });
  33. }
  34. Promise.race([delay2(95), myfetch("https://swapi.dev/api/people/1/")]).then(res => console.log(res));
  35. </script>
  36. </body>
  37. </html>