123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Promise hell</title>
- <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
- <style>
- table{
- font-family: sans-serif;
- border: 1px solid black;
- }
- td{
- border: 1px solid black;
- }
- </style>
- </head>
- <body>
- <div id="cont">
-
- </div>
- <script>
-
-
- function myfetch(url){
- return new Promise(function (resolve, reject){
- let xhr = new XMLHttpRequest()
- xhr.open('GET',url,true);
- xhr.send();
- xhr.onload= () => xhr.status === 200 ? resolve(JSON.parse(xhr.responseText)) : reject();
- });
- }
- myfetch('https://swapi.dev/api/people/1/')
- .then(luke => console.log(luke), () => console.log('Error'));
- function delay(ms){
- return new Promise(resolve=>setTimeout(resolve,ms));
- }
- Promise.race([delay(100),myfetch('https://swapi.dev/api/people/1/')])
- .then(res=>{
- if(!res){
- console.log('hello');
- }else{
- console.log(res);
- }
- },
- err=>{
- console.log(err);
- }
- );
- </script>
- </body>
- </html>
|