main.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //fetch for first task
  2. let table = document.createElement("table")
  3. document.body.append(table)
  4. // fetch('https://swapi.dev/api/people/1/')
  5. // .then(res => res.json())
  6. // .then(luke => createTable(table, luke))
  7. //fetch for second task
  8. function fetchForTable(url) {
  9. fetch(url)
  10. .then(res => res.json())
  11. .then(luke => createTableImproved(newtable, luke))
  12. }
  13. let newtable = document.createElement("table")
  14. document.body.append(newtable)
  15. fetchForTable('https://swapi.dev/api/people/1/')
  16. //fetch basic
  17. function createTable(table, myJson) {
  18. table.innerHTML = ""
  19. for (let [keyJson, valueJson] of Object.entries(myJson)) {
  20. let tr = document.createElement('tr')
  21. //console.log(typeof storeValue)
  22. if (typeof valueJson === "object") {
  23. let thKey = document.createElement('th')
  24. thKey.innerHTML = `${keyJson}`
  25. tr.appendChild(thKey)
  26. for (let [key, value] of Object.entries(valueJson)) {
  27. let thValue = document.createElement('td')
  28. thValue.innerHTML = `${value}`
  29. tr.appendChild(thValue)
  30. }
  31. } else {
  32. let thKey2 = document.createElement('th')
  33. let thValue2 = document.createElement('td')
  34. thKey2.innerHTML = `${keyJson}`;
  35. thValue2.innerHTML = ` ${valueJson}`
  36. tr.appendChild(thKey2)
  37. tr.appendChild(thValue2)
  38. }
  39. table.append(tr)
  40. }
  41. }
  42. //fetch improved
  43. function createTableImproved(table, myJson) {
  44. const substring = 'https://swapi.dev/api/'
  45. for (let [keyJson, valueJson] of Object.entries(myJson)) {
  46. let tr = document.createElement('tr')
  47. if (typeof valueJson === "object") {
  48. let thKey = document.createElement('th')
  49. thKey.innerHTML = `${keyJson}`
  50. tr.appendChild(thKey)
  51. for (let [key, value] of Object.entries(valueJson)) {
  52. if (value.includes(substring)) {
  53. let btnValue = document.createElement('button')
  54. btnValue.style.color = 'red'
  55. btnValue.innerHTML = `<b> ${value}`
  56. btnValue.onclick = () => fetchForTable(value)
  57. tr.appendChild(btnValue)
  58. } else {
  59. let thValue = document.createElement('td')
  60. thValue.innerHTML = `${value}`
  61. tr.appendChild(thValue)
  62. }
  63. }
  64. } else if ((typeof valueJson === "string") && (valueJson.includes(substring))) {
  65. let thKey2 = document.createElement('th')
  66. thKey2.innerHTML = `${keyJson}`;
  67. let btnValue = document.createElement('a')
  68. btnValue.style.color = 'red'
  69. btnValue.innerHTML = `<b> ${valueJson}`
  70. btnValue.onclick = () => fetchForTable(valueJson)
  71. tr.appendChild(thKey2)
  72. tr.appendChild(btnValue)
  73. } else {
  74. let thKey2 = document.createElement('th')
  75. let thValue2 = document.createElement('td')
  76. thKey2.innerHTML = `${keyJson}`;
  77. thValue2.innerHTML = ` ${valueJson}`
  78. tr.appendChild(thKey2)
  79. tr.appendChild(thValue2)
  80. }
  81. table.append(tr)
  82. }
  83. }
  84. //1 вариант
  85. // function myfetch(url) {
  86. // return new Promise(function(resolve, reject) {
  87. // const xhr = new XMLHttpRequest();
  88. // xhr.open('GET', url, true);
  89. // xhr.send();
  90. // xhr.onreadystatechange =
  91. // function() {
  92. // try {
  93. // if (xhr.readyState != 4)
  94. // return;
  95. // if (xhr.status != 200) {
  96. // reject(new Error(`Ошибка ${xhr.status}: ${xhr.statusText}`))
  97. // } else {
  98. // resolve(JSON.parse(xhr.responseText));
  99. // }
  100. // } catch (err) { // для отлова ошибок используем конструкцию try...catch вместо onerror
  101. // reject(new Error('ERROR: Request failed' + `${err}`))
  102. // }
  103. // }
  104. // })
  105. // }
  106. // //2 вариант
  107. function myfetch(url) {
  108. return new Promise(function(resolve, reject) {
  109. const xhr = new XMLHttpRequest();
  110. xhr.open('GET', url, true);
  111. xhr.send();
  112. xhr.onreadystatechange = () => {
  113. if (xhr.readyState != 4)
  114. return;
  115. (xhr.status == 200) ?
  116. resolve(JSON.parse(xhr.responseText)):
  117. reject(new Error(`Ошибка ${xhr.status}: ${xhr.statusText}`))
  118. }
  119. xhr.onerror = () => reject(new Error('ERROR: Request failed'))
  120. })
  121. }
  122. // myfetch('https://swapi.dev/api/people/1/')
  123. // .then(luke => console.log(luke))
  124. //race
  125. const delay = ms => {
  126. return new Promise(resolve => setTimeout(() => resolve(), ms))
  127. }
  128. //delay(2000).then(() => console.log('after 2 sec'))
  129. // Promise.race([delay(10).then(() => console.log('after 10 ms')), myfetch('https://swapi.dev/api/people/1/')
  130. // .then(luke => console.log(luke))
  131. // ]).then(() => console.log('first promise'))
  132. console.log('start promises...')
  133. Promise.race([delay(1000).then(() => console.log('after 1 sec')), myfetch('https://swapi.dev/api/people/1/')
  134. .then(luke => console.log(luke))
  135. ]).then(() => console.log('first promise'))
  136. Promise.all([delay(2000).then(() => console.log('after 2 sec')), myfetch('https://swapi.dev/api/people/1/')
  137. .then(luke => console.log(luke))
  138. ]).then(() => console.log('all promises'))