index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // fetch basic && fetch improved
  2. async function getData(link) {
  3. try {
  4. const data = await fetch(link);
  5. const person = await data.json();
  6. return person;
  7. } catch (e) {
  8. console.log(e);
  9. }
  10. }
  11. async function showTable(block, link) {
  12. let readyTable = await buildTable(block, await getData(link));
  13. let blockEl = document.querySelector(".container");
  14. blockEl.appendChild(readyTable);
  15. }
  16. async function buildTable(el, json) {
  17. let newTable = document.createElement("table");
  18. newTable.className = ".tableFetch";
  19. newTable.setAttribute("border", "1");
  20. let tds = 0;
  21. let btns = 0;
  22. for (let [key, value] of Object.entries(json)) {
  23. tds++;
  24. let th = document.createElement("th");
  25. let td = document.createElement("td");
  26. td.id = `${key}TD${tds}`;
  27. let tr = document.createElement("tr");
  28. th.innerText = key;
  29. if (typeof value === "string" && value.includes("http")) {
  30. btns++;
  31. let btn = document.createElement("button");
  32. btn.id = `${key}Btn${btns}`;
  33. btn.innerText = value;
  34. td.innerHTML = btn.outerHTML;
  35. } else if (Array.isArray(value) && value.length !== 0) {
  36. for (let el of value) {
  37. btns++;
  38. let btn = document.createElement("button");
  39. btn.id = `${key}Btn${btns}`;
  40. btn.innerText = el;
  41. td.innerHTML += `${btn.outerHTML}\n`;
  42. }
  43. } else {
  44. td.innerText = value.length > 0 ? value : "No value";
  45. }
  46. tr.appendChild(th);
  47. tr.appendChild(td);
  48. newTable.append(tr);
  49. }
  50. let buttons = newTable.querySelectorAll("button");
  51. if (buttons.length !== 0) {
  52. for (let btn of buttons) {
  53. btn.onclick = async () => {
  54. let appendTable = document.createElement("table");
  55. appendTable.setAttribute("border", "1");
  56. let btnTR = btn.closest("tr");
  57. let btnTH = btnTR.firstChild;
  58. btnTR.className = `${btnTH.textContent}TR`;
  59. let data = btn.textContent;
  60. btn.remove();
  61. let TR = document.querySelector(`.${btnTR.className}`);
  62. appendTable.appendChild(TR);
  63. await showTable(appendTable, data);
  64. };
  65. }
  66. }
  67. el.appendChild(newTable);
  68. return el;
  69. }
  70. showTable(blockTable, "https://swapi.dev/api/people/1/");
  71. // myfetch
  72. function myfetch(url) {
  73. return new Promise(function (resolve, reject) {
  74. const request = new XMLHttpRequest();
  75. request.open("GET", url, true);
  76. request.onreadystatechange = function () {
  77. if (request.readyState != 4) {
  78. return;
  79. }
  80. if (request.status == 200) {
  81. resolve(JSON.parse(request.responseText));
  82. } else {
  83. reject(request.status + ", " + request.statusText);
  84. }
  85. };
  86. request.send();
  87. });
  88. }
  89. myfetch("https://swapi.dev/api/people/1/").then((data) =>
  90. console.log(`MyFetch response:`, data)
  91. );
  92. // race
  93. const delay = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
  94. Promise.race([delay(1000), myfetch("https://swapi.dev/api/people/1/")]).then(
  95. (value) => {
  96. console.log(`First loaded promise: `, value);
  97. }
  98. );