index.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Promise hell</title>
  6. <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
  7. <style>
  8. table{
  9. font-family: sans-serif;
  10. border: 1px solid black;
  11. }
  12. td{
  13. border: 1px solid black;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="cont">
  19. </div>
  20. <script>
  21. //-----------------fetch Basic-------------------------------
  22. /*function create(container, json){
  23. let table = document.createElement('table');
  24. for(let i in json){
  25. let tr = document.createElement('tr');
  26. let td = document.createElement('td');
  27. let td2 = document.createElement('td');
  28. td.innerText = i;
  29. td2.innerText = json[i];
  30. tr.append(td,td2);
  31. table.append(tr);
  32. }
  33. container.append(table);
  34. }
  35. fetch('https://swapi.dev/api/people/1/')
  36. .then(res => res.json())
  37. .then(res => create(cont,res))*/
  38. //---------------fetch improved--------------------------------
  39. /*function create(container, json) {
  40. let table = document.createElement('table');
  41. for (const i in json) {
  42. let tr = document.createElement('tr');
  43. let td = document.createElement('th');
  44. let td2 = document.createElement('td');
  45. td.innerText = i;
  46. if(typeof json[i] === 'string' && json[i].includes('https://swapi.dev/api/')) {
  47. let btn = document.createElement('button');
  48. btn.innerText = json[i];
  49. let key=false;
  50. btn.addEventListener('click',()=>{
  51. key===true?key=false:key=true;
  52. console.log(key);
  53. if(key==true){
  54. fetch(json[i])
  55. .then(res => res.json())
  56. .then(res => create(td2,res));
  57. }else{
  58. let k=td2.lastElementChild;
  59. td2.removeChild(k);
  60. }
  61. })
  62. td2.append(btn);
  63. }else if(Array.isArray(json[i])) {
  64. for(let item of json[i]){
  65. if(typeof item === 'string' && item.includes('https://swapi.dev/api/')){
  66. let p=document.createElement('p');
  67. let btn = document.createElement('button');
  68. btn.innerText = item;
  69. let key=false;
  70. btn.addEventListener('click',()=>{
  71. key===true?key=false:key=true;
  72. console.log(key);
  73. if(key==true){
  74. fetch(item)
  75. .then(res => res.json())
  76. .then(res => create(td2,res));
  77. }else{
  78. let k=td2.lastElementChild;
  79. td2.removeChild(k);
  80. }
  81. })
  82. p.append(btn);
  83. td2.append(p);
  84. }else{
  85. td2.append = item;
  86. }
  87. }
  88. }else if(typeof json[i] === 'object' && json[i] !== null){
  89. for (const key in jsonStr[key]) {
  90. let obj = document.createElement('p');
  91. obj.textContent = `${i}: ${json[i]}`;
  92. td2.append(obj)
  93. }
  94. }else{
  95. td2.innerText = json[i];
  96. }
  97. tr.append(td);
  98. tr.append(td2);
  99. table.append(tr);
  100. for (let cell of tr.cells) {
  101. cell.style.border = '2px solid black';
  102. cell.style.padding = '10px';
  103. cell.style.textAlign = 'left';
  104. }
  105. }
  106. container.append(table);
  107. }
  108. fetch('https://swapi.dev/api/people/1/')
  109. .then(res => res.json())
  110. .then(res => create(cont, res));*/
  111. //------------myFetch
  112. function myfetch(url){
  113. return new Promise(function (resolve, reject){
  114. let xhr = new XMLHttpRequest()
  115. xhr.open('GET',url,true);
  116. xhr.send();
  117. xhr.onload= () => xhr.status === 200 ? resolve(JSON.parse(xhr.responseText)) : reject();
  118. });
  119. }
  120. myfetch('https://swapi.dev/api/people/1/')
  121. .then(luke => console.log(luke), () => console.log('Error'));
  122. //------------race
  123. function delay(ms){
  124. return new Promise(resolve=>setTimeout(resolve,ms));
  125. }
  126. Promise.race([delay(100),myfetch('https://swapi.dev/api/people/1/')])
  127. .then(res=>{
  128. if(!res){
  129. console.log('hello');
  130. }else{
  131. console.log(res);
  132. }
  133. },
  134. err=>{
  135. console.log(err);
  136. }
  137. );
  138. </script>
  139. </body>
  140. </html>