script.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. function createBtn(text) {
  2. let btn = document.createElement('button')
  3. btn.innerText = `${text}`
  4. return btn
  5. }
  6. function drawObjTable(parent, obj) {
  7. let table = document.createElement('table')
  8. table.border = 1
  9. table.style.border = '5px double grey'
  10. let closeRow = document.createElement('tr')
  11. let closeTd = document.createElement('td')
  12. closeTd.style.border = 'none'
  13. let closeBtn = createBtn('Close table')
  14. closeBtn.style.backgroundColor = 'firebrick'
  15. closeBtn.style.color = 'white'
  16. closeBtn.onclick = () => parent.removeChild(table)
  17. closeTd.append(closeBtn)
  18. closeRow.append(closeTd)
  19. table.append(closeRow)
  20. for (let key in obj) {
  21. let tr = document.createElement('tr')
  22. let tdKey = document.createElement('td')
  23. tdKey.innerText = key
  24. tdKey.style.backgroundColor = 'black'
  25. tdKey.style.color = 'white'
  26. let tdValue = document.createElement('td')
  27. tdValue.style.backgroundColor = 'grey'
  28. tdValue.style.color = 'whitesmoke'
  29. if (obj[key].constructor.name === 'Array') {
  30. for (let item of obj[key]) {
  31. if (typeof item === 'string' && /^(https:\/\/|http:\/\/)/.test(item)) {
  32. let btn = createBtn(`Loading...`)
  33. btn.disabled = true
  34. fetch(item)
  35. .then((res) => {
  36. if(res.ok) {
  37. return res.json()
  38. } else {
  39. throw new Error()
  40. }
  41. })
  42. .then(obj => {
  43. btn.disabled = false
  44. btn.innerText = obj[Object.keys(obj)[0]]
  45. btn.onclick = () => { drawObjTable(container, obj) }
  46. })
  47. .catch(e => console.warn('something bad happened ', e))
  48. tdValue.append(btn)
  49. } else {
  50. tdValue.innerText = item
  51. }
  52. }
  53. } else if (typeof obj[key] === 'string' && /^(https:\/\/|http:\/\/)/.test(obj[key])) {
  54. let btn = createBtn(key)
  55. btn.disabled = true
  56. fetch(obj[key])
  57. .then((res) => {
  58. if(res.ok) {
  59. return res.json()
  60. } else {
  61. throw new Error()
  62. }
  63. })
  64. .then(obj => {
  65. btn.disabled = false
  66. btn.innerText = obj[Object.keys(obj)[0]]
  67. btn.onclick = () => { drawObjTable(container, obj) }
  68. })
  69. .catch(e => console.warn('something bad happened ', e))
  70. tdValue.append(btn)
  71. } else {
  72. tdValue.innerText = obj[key]
  73. }
  74. tr.append(tdKey, tdValue)
  75. table.append(tr)
  76. }
  77. parent.append(table)
  78. }
  79. let container = document.createElement('div')
  80. document.body.append(container)
  81. //luke skywalker
  82. fetch('https://swapi.dev/api/people/1/')
  83. .then((res) => {
  84. if(res.ok) {
  85. return res.json()
  86. } else {
  87. throw new Error()
  88. }
  89. })
  90. .then(luke => {
  91. drawObjTable(container, luke)
  92. })
  93. .catch((e) => console.warn('something bad happpened ', e))
  94. // myFetch
  95. function myFetch(url) {
  96. return new Promise((resolve, reject) => {
  97. const xhr = new XMLHttpRequest()
  98. xhr.open('GET', url, true)
  99. xhr.onreadystatechange = () => {
  100. if (xhr.readyState != 4){
  101. return;
  102. }
  103. if (xhr.status == 200){
  104. resolve(JSON.parse(xhr.response))
  105. }
  106. else {
  107. reject(new Error(`smth bad happened during request to ${url}. ${xhr.responseText}`))
  108. }
  109. }
  110. xhr.onerror = () => {
  111. reject(new Error(`smth bad happened during request to ${url}. ${xhr.responseText}`))
  112. }
  113. xhr.send()
  114. })
  115. }
  116. //darth vader
  117. myFetch('https://swapi.dev/api/people/4/')
  118. .then((res) => {
  119. drawObjTable(container, res)
  120. })
  121. .catch((e) => {console.warn('error occured!', e)})
  122. //let builtIn = fetch('https://swapi.dev/api/people/1/')
  123. let mine = myFetch('https://swapi.dev/api/people/4/')
  124. //при нормальном инете сначал побеждает дилей потом майн всегда
  125. //let delayPromise = delay(300)
  126. //Promise.race([builtIn, mine]).then((value) => console.log('fastest', value))
  127. function delay(ms) {
  128. return new Promise((resolve, reject) => {
  129. setTimeout(resolve, ms);
  130. });
  131. }
  132. //вот таким образом выходит по разному при условии что симулируется slow 3g
  133. //по разному но не 50 на 50.
  134. let delayPromise = delay(4299)
  135. Promise.race([delayPromise, mine]).then((value) => console.log('fastest:', value || 'delay'))