index.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="LukeSkywalker"></div>
  11. <script>
  12. // ****** fetch basic-fetch improved1 *******//
  13. div = document.getElementById('LukeSkywalker')
  14. function darkSide(DOM, JASON) {
  15. let obj = JASON
  16. let table = document.createElement('table')
  17. table.setAttribute('style', "display: inline-block; border:1px; margin: 30px 15px; border-style:solid; border-color:#FF0000;")
  18. let stringUrl = 'https://swapi.dev/api/'
  19. for (const iterator in obj) {
  20. let row = document.createElement('tr')
  21. row.setAttribute('style', 'padding:15px;')
  22. row.innerHTML = iterator.toUpperCase() + ":";
  23. table.appendChild(row)
  24. let cell = document.createElement('td')
  25. //console.log(obj[iterator])
  26. try {
  27. if (obj[iterator].indexOf(stringUrl) > -1) {
  28. let button = document.createElement('button')
  29. button.innerHTML = "show"
  30. cell.appendChild(button)
  31. button.onclick = function () {
  32. fetch(`${obj[iterator]}`)
  33. .then(res => res.json())
  34. .then(anyJson => darkSide(DOM, anyJson))
  35. }
  36. }
  37. else if (typeof obj[iterator] === 'object') {
  38. for (let j = 0; j < obj[iterator].length; j++) {
  39. let button = document.createElement('button')
  40. button.innerHTML = "show"
  41. button.onclick = function () {
  42. fetch(`${obj[iterator][j]}`)
  43. .then(res => res.json())
  44. .then(anyJson => darkSide(DOM, anyJson))
  45. }
  46. cell.appendChild(button)
  47. }
  48. } else
  49. cell.innerHTML = obj[iterator]
  50. cell.setAttribute('style', "border: 1px; border-style: solid; border-color:#FF0000; text-align: center; ")
  51. row.appendChild(cell)
  52. }
  53. catch (e) {
  54. console.log(e)
  55. }
  56. }
  57. DOM.appendChild(table)
  58. }
  59. fetch('https://swapi.dev/api/people/1/')
  60. .then(res => res.json())
  61. .then(luke => darkSide(div, luke))
  62. //******** myFetch ************//
  63. function myfetch(url) {
  64. return new Promise(function (resolve, reject) {
  65. const xhr = new XMLHttpRequest();
  66. xhr.open("GET", url);
  67. xhr.responseType = 'json'
  68. xhr.onload = () => {
  69. if (xhr.status >= 400) {
  70. reject(xhr.response)
  71. } else {
  72. resolve(xhr.response)
  73. }
  74. }
  75. xhr.send();
  76. })
  77. }
  78. myfetch('https://swapi.dev/api/people/1/')
  79. .then(luke => console.log(luke))
  80. //******** race ************//
  81. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  82. console.log(Promise.race([myfetch('https://swapi.dev/api/people/1/'), delay(Math.random() * 1000)]))
  83. </script>
  84. </body>
  85. </html>