main.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. function createTable(parent,data){
  2. /* let response = await fetch('https://swapi.py4e.com/api/people/1/') */
  3. /* .then(res => res.json())
  4. .then(luke => console.log(luke),createTable(document.body, luke)) */
  5. /* let data = await response.json(); */
  6. let table = document.createElement('table');
  7. for(let key in data){
  8. let rows = document.createElement('tr');
  9. let cols = document.createElement('td');
  10. rows.innerHTML = `${key}`
  11. rows.appendChild(cols);
  12. var matcher = /^(?:\w+:)?\/\/([^\s\.]+\.\S{2}|localhost[\:?\d]*)\S*$/;
  13. /* console.log(data); */
  14. function isUrl(string){
  15. return matcher.test(string);
  16. }
  17. /* function isValidURL(string) {
  18. var res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
  19. return (res !== null)
  20. }; */
  21. /* if(Array.isArray(data[key])){
  22. console.log(`${key}`);
  23. let button = document.createElement('button');
  24. data[key].map((element)=>{
  25. console.log(`i'am element in arr ${element}`)
  26. button.innerText = element;
  27. })
  28. //button.innerText = `${data[key]}`
  29. } */
  30. let button= document.createElement('button');
  31. button.id = 'button';
  32. if(isUrl(data[key])){
  33. console.log(`${key}`);
  34. /* let button= document.createElement('button'); */
  35. let div = document.createElement('div');
  36. if(Array.isArray(data[key])){
  37. data[key].map((element)=>{
  38. let btn = document.createElement('button');
  39. console.log(`i'am element in arr ${element}`)
  40. btn.innerText = element;
  41. cols.appendChild(btn);
  42. /* btn.onclick = () =>{
  43. fetch(element)
  44. .then((res)=> res.json)
  45. .then((data) => createTable(div,data));
  46. } */
  47. btn.addEventListener('click', function fetching(){
  48. fetch(element)
  49. .then((res) => res.json())
  50. .then((data) => createTable(div,data))
  51. })
  52. })
  53. //button.innerText = `${data[key]}`
  54. } else{
  55. button.innerText = `${data[key]}`
  56. cols.appendChild(button);
  57. button.addEventListener('click', function fetching(){
  58. fetch(data[key])
  59. .then((res)=> res.json())
  60. .then((data) => createTable(div,data));
  61. })
  62. }
  63. cols.appendChild(div);
  64. } else {
  65. cols.innerHTML = `${data[key]}`
  66. }
  67. /* if(Array.isArray(data[key])){
  68. console.log(`${key}`);
  69. let button = document.createElement('button');
  70. button.innerText = `${data[key]}`
  71. cols.appendChild(button);
  72. } else {} */
  73. table.appendChild(rows);
  74. parent.appendChild(table);
  75. }
  76. }
  77. /* (async () => {
  78. let response = await fetch('https://swapi.py4e.com/api/people/1/');
  79. let data = await response.json
  80. }) */
  81. fetch('https://swapi.py4e.com/api/people/1/')
  82. .then((res)=> res.json())
  83. .then((data) =>new createTable(document.body, data) );
  84. function myfetch(url){
  85. return new Promise(function (resolve, reject){
  86. let xhr = new XMLHttpRequest();
  87. xhr.open('GET',url,true);
  88. xhr.send()
  89. xhr.onreadystatechange = function() {
  90. if (xhr.readyState != 4) return;
  91. if(xhr.status == 200){
  92. console.log('ok all working');
  93. try{
  94. resolve(result = JSON.parse(xhr.responseText))
  95. } catch(e){
  96. console.log(`something wrong ${e.message}`);
  97. }
  98. } else{
  99. reject(alert(xhr.status + ':' + xhr.statusText))
  100. }
  101. }
  102. })
  103. }
  104. myfetch('https://swapi.py4e.com/api/people/1/')
  105. .then(luke => console.log(luke))
  106. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  107. Promise.race([myfetch('https://swapi.py4e.com/api/people/1/'),delay(135)])
  108. .then((firstResult)=> console.log('i`m first on this race guys ' + firstResult));