script.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. function table (element,info){
  2. function clicker (button,el){
  3. button.onclick=()=>{
  4. fetch(el)
  5. .then(res => res.json())
  6. .then(luke =>table(sex,luke))
  7. };
  8. };
  9. for(let i in info){
  10. let tr = document.createElement('tr');
  11. let th = document.createElement('th');
  12. let td = document.createElement('td');
  13. let button = document.createElement('button');
  14. th.innerText=i;
  15. tr.appendChild(th);
  16. if (typeof info[i]==='string'&&info[i].indexOf("http://swapi.dev/api/")===0){
  17. td.appendChild(button);
  18. button.classList.add('linkButton');
  19. button.innerText='link';
  20. clicker(button,info[i]);
  21. }else if(typeof info[i]==='object'){
  22. for(let key of info[i]){
  23. td.appendChild(button)
  24. button.innerText='oppen array';
  25. clicker(button,key);
  26. }
  27. }else{
  28. td.innerText =info[i];
  29. }
  30. tr.appendChild(td);
  31. element.appendChild(tr);
  32. }
  33. }
  34. fetch('https://swapi.dev/api/people/1/')
  35. .then(res => res.json())
  36. .then(luke => table(box,luke))
  37. function myFetch(url){
  38. return new Promise(function (resolve, reject){
  39. const xhr = new XMLHttpRequest();
  40. xhr.open('GET', url, true)
  41. xhr.onload =function (){
  42. if(xhr.status != 200){
  43. reject(`Ошибка ${xhr.status}: ${xhr.statusText}`)
  44. }else{
  45. resolve(JSON.parse(xhr.responseText));
  46. }
  47. }
  48. xhr.onerror = () => alert("Запрос не удался")
  49. xhr.send();
  50. });
  51. }
  52. myFetch('https://swapi.dev/api/starships/9/')
  53. .then(luke => console.log(luke))
  54. const promise = new Promise(function (resolve, reject) {
  55. myFetch('https://swapi.dev/api/people/1/')
  56. .then(() => resolve('Promise'))
  57. })
  58. const delay = ms => new Promise(ok => setTimeout(() => ok("delay"), ms))
  59. Promise.race([promise, delay(300)]).then(function (value) {
  60. console.log(`${value} win`);
  61. })