function createTable(element,information){ for(let i in information){ let tr = document.createElement('tr'); let th = document.createElement('th'); let td = document.createElement('td'); th.innerText=i; td.innerText = information[i]; tr.appendChild(th); tr.appendChild(td); element.appendChild(tr); }; }; fetch('https://swapi.dev/api/starships/9/') .then(res => res.json()) .then(luke =>createTable(assignment,luke)) function createImproved(element,information){ function clickBtn(btn,el){ btn.onclick=()=>{ fetch(el) .then(res => res.json()) .then(luke =>createImproved(improved,luke)) }; }; for(let i in information){ let tr = document.createElement('tr'); let th = document.createElement('th'); let td = document.createElement('td'); th.innerText=i; tr.appendChild(th); if (typeof information[i]==='string'&&information[i].indexOf("http://swapi.dev/api/")===0){ let btn = document.createElement('button'); td.appendChild(btn); btn.classList.add('linkBtn'); btn.innerText='link'; clickBtn(btn,information[i]); } else if(typeof information[i]==='object'){ for(let key of information[i]){ let btn = document.createElement('button'); td.appendChild(btn) btn.innerText='oppen array'; clickBtn(btn,key); } } else { td.innerText =information[i]; } tr.appendChild(td); element.appendChild(tr); }; }; fetch('https://swapi.dev/api/starships/9/') .then(res => res.json()) .then(luke =>createImproved(improved,luke)) function mFetch(url){ return new Promise(function (res, rej){ const xxx = new XMLHttpRequest(); xxx.open("GET", url,true); xxx.onload = () =>{ if (xxx.status != 200){ rej(xxx.response); }else { res(JSON.parse(xxx.responseText)); }; }; xxx.onerror = () => reject(xhr.statusText); xxx.send(); }); }; mFetch('https://swapi.dev/api/starships/9/') .then(luke => console.log(luke)) const promise1 = new Promise(function(resolve,reject){ mFetch('https://swapi.dev/api/starships/9/') .then(() =>resolve('promise1') ) }); const promise2 = ms => new Promise(ok => setTimeout(() => ok("promise2"), ms)); Promise.race([promise1, promise2(120)]).then(function(value) { console.log(value); });