index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* fetch basic & fetch improved*/
  2. let result = document.body
  3. fetch('https://swapi.py4e.com/api/people/1/')
  4. .then(res => res.json())
  5. .then(luke => createTable(result, luke));
  6. function createTable(result, JSON) {
  7. let table = document.createElement("table");
  8. function createCol(title, value) {
  9. let tr = document.createElement("tr");
  10. let td = document.createElement("td");
  11. let td1 = document.createElement("td");
  12. td.innerHTML = title;
  13. if (typeof value === "object") {
  14. if (Array.isArray(value)) {
  15. value.forEach(elem => {
  16. td1.appendChild(elem)
  17. }
  18. );
  19. }
  20. else {
  21. td1.appendChild(value)
  22. }
  23. } else {
  24. td1.innerHTML = value;
  25. }
  26. table.append(tr);
  27. tr.append(td, td1);
  28. }
  29. for (let [key, value] of Object.entries(JSON)) {
  30. createCol(key, link(value));
  31. }
  32. result.append(table);
  33. table.border = 1;
  34. }
  35. function link(str, container) {
  36. if (typeof str === 'string') {
  37. if (str.includes('https://')) {
  38. let btn = document.createElement('button')
  39. btn.innerHTML = 'Go'
  40. btn.onclick = () => {
  41. fetch(str).then(r => r.json())
  42. .then(r => createTable(btn.parentElement, r))
  43. }
  44. return btn
  45. } else {
  46. return str
  47. }
  48. }
  49. else if (typeof str === 'object') {
  50. let arrStr = str.map(item => {
  51. if (item.includes('https://')) {
  52. let btn = document.createElement('button')
  53. btn.innerHTML = 'Go'
  54. btn.onclick = () => {
  55. fetch(item).then(r => r.json())
  56. .then(r => createTable(btn.parentElement, r))
  57. }
  58. return btn
  59. } else {
  60. return item
  61. }
  62. })
  63. return arrStr
  64. }
  65. else {
  66. return str
  67. }
  68. }
  69. /* myfetch */
  70. myfetch('https://swapi.dev/api/people/1/')
  71. .then(luke => console.log(luke))
  72. function myfetch(url){
  73. return new Promise(function (resolve, reject){
  74. const xhr = new XMLHttpRequest();
  75. xhr.onreadystatechange = function() {
  76. if (xhr.readyState != 4) {
  77. return;
  78. }
  79. if (xhr.status == 200){
  80. resolve(JSON.parse(xhr.responseText));
  81. } else {
  82. reject('err')
  83. }
  84. }
  85. xhr.open('GET', url , true);
  86. xhr.send(null);
  87. })
  88. }
  89. /* race */
  90. let luke1 = new Promise((resolve, reject) => {
  91. fetch('https://swapi.dev/api/people/1/')
  92. .then(res => res.json())
  93. .then(luke => console.log(luke))
  94. });
  95. let delay = new Promise((resolve, reject) => {
  96. setTimeout(resolve, Math.random() * 1000, 'delay');
  97. });
  98. Promise.race([luke1, delay]).then((x) => {
  99. console.log(x);
  100. });