123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 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);
- });
|