script.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //fetch basic / fetch improved
  2. function fetchBasic(element, jsonStr) {
  3. let table = document.createElement('table');
  4. table.style.fontFamily = 'sans-serif';
  5. for (const key in jsonStr) {
  6. let tr = document.createElement('tr');
  7. let tdKey = document.createElement('th');
  8. let tdValue = document.createElement('td');
  9. tdKey.textContent = key;
  10. if(typeof jsonStr[key] === 'string' && jsonStr[key].includes('https://swapi.dev/api/')) {
  11. let button = document.createElement('button');
  12. button.textContent = jsonStr[key];
  13. button.onclick = () => {
  14. fetch(jsonStr[key])
  15. .then(res => res.json())
  16. .then(luke => fetchBasic(tdValue, luke));
  17. }
  18. button.style.border = 'transparent';
  19. button.style.backgroundColor = 'transparent';
  20. button.style.fontSize = '16px'
  21. button.style.padding = '0'
  22. button.style.cursor = 'pointer'
  23. tdValue.append(button);
  24. }
  25. else if(Array.isArray(jsonStr[key])) {
  26. jsonStr[key].forEach((item) =>{
  27. if (typeof item === 'string' && item.includes('https://swapi.dev/api/')){
  28. let button = document.createElement('button');
  29. button.textContent = item;
  30. button.onclick = () => {
  31. fetch(item)
  32. .then(res => res.json())
  33. .then(luke => fetchBasic(tdValue, luke));
  34. }
  35. button.style.border = 'transparent';
  36. button.style.backgroundColor = 'transparent';
  37. button.style.fontSize = '16px'
  38. button.style.padding = '0'
  39. button.style.cursor = 'pointer'
  40. tdValue.append(button);
  41. }
  42. else {
  43. tdValue.append = item
  44. }
  45. })
  46. }
  47. else if(typeof jsonStr[key] === 'object' && jsonStr[key] !== null){
  48. for (const key in jsonStr[key]) {
  49. let obj = document.createElement('p');
  50. obj.textContent = `${key}: ${jsonStr[key]}`;
  51. tdValue.append(obj)
  52. }
  53. }
  54. else{
  55. tdValue.textContent = jsonStr[key];
  56. }
  57. tr.append(tdKey);
  58. tr.append(tdValue);
  59. table.append(tr);
  60. for (let cell of tr.cells) {
  61. cell.style.border = '2px solid #000';
  62. cell.style.padding = '10px';
  63. cell.style.textAlign = 'left';
  64. cell.classList.add('td');
  65. }
  66. }
  67. element.append(table);
  68. }
  69. fetch('https://swapi.dev/api/people/1/')
  70. .then(res => res.json())
  71. .then(luke => fetchBasic(containerForm, luke));
  72. //myfetch
  73. function myfetch(url){
  74. return new Promise(function (resolve, reject){
  75. let xhr = new XMLHttpRequest();
  76. xhr.open("GET", url, true);
  77. xhr.send();
  78. xhr.onload = () => xhr.status !== 200 ? reject() : resolve(JSON.parse(xhr.responseText));
  79. xhr.onerror = () => reject();
  80. })
  81. }
  82. myfetch('https://swapi.dev/api/people/1/')
  83. .then(luke => console.log(luke), () => console.log('Error:'))
  84. //race
  85. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));
  86. Promise.race([myfetch('https://swapi.dev/api/people/1/'), delay(2080)])
  87. .then(value => console.log(value), () => console.log('Error'));