main.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // fetch basic
  2. // fetch improved
  3. fetch('https://swapi.dev/api/people/1/')
  4. .then(res => res.json())
  5. .then(luke => createHero(document.body, luke))
  6. function createHero(parent, data) {
  7. let table = document.createElement('table')
  8. table.style.border = '2px solid #808080'
  9. table.style.borderCollapse = 'collapse'
  10. table.style.background = '#FFFAF0'
  11. parent.append(table)
  12. for (let key in data) {
  13. let tr = document.createElement('tr')
  14. table.append(tr)
  15. let th = document.createElement('th')
  16. th.style.border = '2px solid #808080'
  17. tr.append(th)
  18. th.append(key)
  19. let td = document.createElement('td')
  20. td.style.border = '2px solid #808080'
  21. tr.append(td)
  22. let btn = function(parent, link) {
  23. button = document.createElement('button')
  24. button.style.background = '#AFEEEE'
  25. button.style.color = '#1E90FF'
  26. button.append(link)
  27. parent.append(button)
  28. button.onclick = () => {
  29. fetch(link)
  30. .then(res => res.json())
  31. .then(luke => createHero(document.body, luke))
  32. }
  33. }
  34. if (data[key] instanceof Array) {
  35. for (let arr of data[key]) {
  36. if (arr.startsWith('https://swapi.dev/api/')) {
  37. btn(td, arr)
  38. }
  39. else td.append(data[key])
  40. }
  41. }
  42. else {
  43. if (typeof data[key] === 'string' && data[key].startsWith('https://swapi.dev/api/')) {
  44. btn(td, data[key])
  45. }
  46. else td.append(data[key])
  47. }
  48. }
  49. }
  50. // myfetch
  51. // example1
  52. function myfetch(url) {
  53. return new Promise(function (resolve, reject) {
  54. const xhr = new XMLHttpRequest()
  55. xhr.open('GET', url)
  56. xhr.onload = () => {
  57. if (xhr.status != 200) {
  58. reject(`Error: ${xhr.statusText}`)
  59. return
  60. }
  61. resolve(JSON.parse(xhr.response))
  62. }
  63. xhr.onerror = function() {
  64. reject('Error')
  65. }
  66. xhr.send()
  67. })
  68. }
  69. myfetch('https://swapi.dev/api/people/1/')
  70. .then(luke => console.log(luke)) // вывод в консоль
  71. // or
  72. // example2
  73. function myfetch(url) {
  74. return new Promise(function (resolve, reject) {
  75. const xhr = new XMLHttpRequest
  76. xhr.open('GET', url, true)
  77. xhr.onreadystatechange = function() {
  78. if (xhr.readyState !== 4) {
  79. return;
  80. }
  81. if (xhr.status !== 200) {
  82. reject(console.log('Error' + this.statusText))
  83. }
  84. else {
  85. resolve(JSON.parse(xhr.responseText))
  86. }
  87. }
  88. xhr.send()
  89. })
  90. }
  91. myfetch('https://swapi.dev/api/people/1/')
  92. .then(createHero => console.log(createHero)) // вывод в консоль
  93. // myfetch('https://swapi.dev/api/people/1/')
  94. // .then(json => createHero(document.body, json)) // для отрисовки на странице
  95. // race
  96. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  97. Promise.race([myfetch('https://swapi.dev/api/people/1/'),delay(100)]).then(res => console.log(res))