main.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // fetch basic
  2. {
  3. let DOM = document.body;
  4. fetch('https://swapi.py4e.com/api/people/1/')
  5. .then(res => res.json(),
  6. err => console.log("ERROR"))
  7. .then(luke => createTable(DOM, luke));
  8. function createTable(DOM, JSON) {
  9. console.log(DOM, JSON);
  10. const tableEl = document.createElement("table");
  11. tableEl.border = 1;
  12. for (let [key, value] of Object.entries(JSON)) {
  13. createTr(key, value);
  14. }
  15. DOM.append(tableEl);
  16. function createTr(td1, td2) {
  17. const trEl = document.createElement("tr");
  18. const td1El = document.createElement("td");
  19. const td2El = document.createElement("td");
  20. td1El.innerHTML = td1;
  21. if (typeof td2 === "object") {
  22. const ulEl = document.createElement("ul");
  23. for (let value of td2) {
  24. const liEl = document.createElement("li");
  25. liEl.innerText = value;
  26. ulEl.appendChild(liEl);
  27. }
  28. td2El.appendChild(ulEl);
  29. } else {
  30. td2El.innerHTML = td2;
  31. }
  32. trEl.append(td1El, td2El);
  33. tableEl.append(trEl);
  34. }
  35. }
  36. }
  37. // fetch improved
  38. {
  39. let DOM = document.body;
  40. myFetch(DOM);
  41. function myFetch(dom) {
  42. fetch('https://swapi.py4e.com/api/people/1/')
  43. .then(res => res.json(),
  44. err => console.log("ERROR"))
  45. .then(luke => createTable(dom, luke));
  46. }
  47. function createTable(DOM, JSON) {
  48. console.log(DOM, JSON);
  49. const tableEl = document.createElement("table");
  50. tableEl.border = 1;
  51. for (let [key, value] of Object.entries(JSON)) {
  52. createTr(key, value);
  53. }
  54. DOM.append(tableEl);
  55. function createTr(td1, td2) {
  56. const trEl = document.createElement("tr");
  57. const td1El = document.createElement("td");
  58. const td2El = document.createElement("td");
  59. td1El.innerHTML = td1;
  60. if (typeof td2 === "number") td2 = td2.toString();
  61. if (typeof td2 !== "object" && !td2.indexOf("https://swapi.py4e.com/api/")) td2 = [td2];
  62. if (typeof td2 === "object") {
  63. const ulEl = document.createElement("ul");
  64. for (let value of td2) {
  65. const liEl = document.createElement("li");
  66. const buttonEl = document.createElement("button");
  67. buttonEl.innerText = value;
  68. buttonEl.onclick = () => {
  69. buttonEl.hidden = true;
  70. myFetch(buttonEl.parentElement);
  71. }
  72. liEl.appendChild(buttonEl);
  73. ulEl.appendChild(liEl);
  74. }
  75. td2El.appendChild(ulEl);
  76. } else {
  77. td2El.innerHTML = td2;
  78. }
  79. trEl.append(td1El, td2El);
  80. tableEl.append(trEl);
  81. }
  82. }
  83. }
  84. // myfetch
  85. myfetch('https://swapi.py4e.com/api/people/1/').then(luke => console.log(luke));
  86. function myfetch(url) {
  87. return new Promise(function (resolve, reject) {
  88. const xhr = new XMLHttpRequest();
  89. xhr.open('GET', url, true);
  90. xhr.responseType = "json";
  91. xhr.onload = function () {
  92. if (xhr.status == 200) {
  93. resolve(xhr.response);
  94. }
  95. else {
  96. reject(`ERROR ${xhr.status} - ${xhr.statusText}`);
  97. }
  98. }
  99. xhr.onerror = () => reject(`ERROR ${xhr.status} - ${xhr.statusText}`);
  100. xhr.send();
  101. })
  102. }
  103. // race
  104. Promise.race([myfetch('https://swapi.py4e.com/api/people/1/'), delay(30)]).then((value) => console.log(value));
  105. function myfetch(url) {
  106. return new Promise(function (resolve, reject) {
  107. const xhr = new XMLHttpRequest();
  108. xhr.open('GET', url, true);
  109. xhr.responseType = "json";
  110. xhr.onload = function () {
  111. if (xhr.status == 200) {
  112. resolve(xhr.response);
  113. }
  114. else {
  115. reject(`ERROR ${xhr.status} - ${xhr.statusText}`);
  116. }
  117. }
  118. xhr.onerror = () => reject(`ERROR ${xhr.status} - ${xhr.statusText}`);
  119. xhr.send();
  120. })
  121. }