123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body>
- <script>
- Promise.race([myfetch('https://swapi.py4e.com/api/people/1/'),delay(30)]).then((value) => console.log(value));
- function myfetch(url){
- return new Promise(function (resolve, reject){
- const xhr = new XMLHttpRequest();
- xhr.open('GET', url, true);
- xhr.responseType = "json";
- xhr.onload = function(){
- if (xhr.status == 200){
- resolve(xhr.response);
- }
- else {
- reject(`ERROR ${xhr.status} - ${xhr.statusText}`);
- }
- }
- xhr.onerror = () => reject(`ERROR ${xhr.status} - ${xhr.statusText}`);
- xhr.send();
- })
- }
- function delay(ms) {
- return new Promise(function(resolve,reject) {
- setTimeout(resolve,ms,"delay");
- })
- }
- </script>
- </body>
- </html>
|