|
@@ -0,0 +1,65 @@
|
|
|
+function table (element,info){
|
|
|
+ function clicker (button,el){
|
|
|
+ button.onclick=()=>{
|
|
|
+ fetch(el)
|
|
|
+ .then(res => res.json())
|
|
|
+ .then(luke =>table(sex,luke))
|
|
|
+ };
|
|
|
+ };
|
|
|
+ for(let i in info){
|
|
|
+ let tr = document.createElement('tr');
|
|
|
+ let th = document.createElement('th');
|
|
|
+ let td = document.createElement('td');
|
|
|
+ let button = document.createElement('button');
|
|
|
+ th.innerText=i;
|
|
|
+ tr.appendChild(th);
|
|
|
+ if (typeof info[i]==='string'&&info[i].indexOf("http://swapi.dev/api/")===0){
|
|
|
+ td.appendChild(button);
|
|
|
+ button.classList.add('linkButton');
|
|
|
+ button.innerText='link';
|
|
|
+ clicker(button,info[i]);
|
|
|
+ }else if(typeof info[i]==='object'){
|
|
|
+ for(let key of info[i]){
|
|
|
+ td.appendChild(button)
|
|
|
+ button.innerText='oppen array';
|
|
|
+ clicker(button,key);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ td.innerText =info[i];
|
|
|
+ }
|
|
|
+ tr.appendChild(td);
|
|
|
+ element.appendChild(tr);
|
|
|
+ }
|
|
|
+}
|
|
|
+fetch('https://swapi.dev/api/people/1/')
|
|
|
+ .then(res => res.json())
|
|
|
+ .then(luke => table(box,luke))
|
|
|
+
|
|
|
+function myFetch(url){
|
|
|
+ return new Promise(function (resolve, reject){
|
|
|
+ const xhr = new XMLHttpRequest();
|
|
|
+ xhr.open('GET', url, true)
|
|
|
+ xhr.onload =function (){
|
|
|
+ if(xhr.status != 200){
|
|
|
+ reject(`Ошибка ${xhr.status}: ${xhr.statusText}`)
|
|
|
+ }else{
|
|
|
+ resolve(JSON.parse(xhr.responseText));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ xhr.onerror = () => alert("Запрос не удался")
|
|
|
+ xhr.send();
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+myFetch('https://swapi.dev/api/starships/9/')
|
|
|
+.then(luke => console.log(luke))
|
|
|
+
|
|
|
+const promise = new Promise(function (resolve, reject) {
|
|
|
+ myFetch('https://swapi.dev/api/people/1/')
|
|
|
+ .then(() => resolve('Promise'))
|
|
|
+})
|
|
|
+const delay = ms => new Promise(ok => setTimeout(() => ok("delay"), ms))
|
|
|
+
|
|
|
+Promise.race([promise, delay(300)]).then(function (value) {
|
|
|
+ console.log(`${value} win`);
|
|
|
+})
|