Browse Source

async_promise

Iryna Bolbat 2 years ago
parent
commit
8d2de53cfc
3 changed files with 129 additions and 0 deletions
  1. 13 0
      js_11_async_promise/index.html
  2. 100 0
      js_11_async_promise/main.js
  3. 16 0
      js_11_async_promise/style.css

+ 13 - 0
js_11_async_promise/index.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <link rel="stylesheet" href="style.css">
+    <title>Document</title>
+</head>
+<body>
+    <script src="main.js"></script>
+</body>
+</html>

+ 100 - 0
js_11_async_promise/main.js

@@ -0,0 +1,100 @@
+//fetch basic, fetch improved
+// let table = document.createElement('table');
+// let tr = document.createElement('tr');
+// let th = document.createElement('th');
+// let th2 = document.createElement('th');
+
+// document.body.appendChild(table);
+// table.appendChild(tr);
+// tr.appendChild(th);
+// tr.appendChild(th2);
+// th.innerHTML = 'Key';
+// th2.innerHTML = 'Value';
+
+// let luke = new Promise(function (resolve, reject) {
+//     resolve(fetch('https://swapi.dev/api/people/1/'));
+// })
+
+// luke.then(resolve => resolve.json())
+//     .then(luke => createTable(luke, table));
+
+// function createTable(json, dom){
+//     let url = 'https://swapi.dev/api/'
+//     console.log(json)
+
+//     for(let [key, value] of Object.entries(json)){
+//         let tr = document.createElement('tr');
+//         let tdKey = document.createElement('td');
+//         let tdVal = document.createElement('td');
+//         tdKey.innerHTML = key;
+
+//         if(value.includes(url)){
+//             tdVal.appendChild(createBtn(value, tdVal));
+
+//         } else if(typeof (value) === 'object') {     
+//             value.forEach((el) =>{
+//                 tdVal.appendChild(createBtn(el, tdVal));
+//             })
+//         } else {
+//             tdVal.innerHTML = value;
+//         }
+
+//         dom.appendChild(tr);
+//         tr.appendChild(tdKey);
+//         tr.appendChild(tdVal);
+//     }
+// }
+
+// function createBtn(fetchLink, tdV){
+//     let btn = document.createElement('button');
+//     btn.innerHTML = 'btn';
+//     btn.onclick = function(){
+//         fetch(fetchLink)
+//             .then(resolve => resolve.json())
+//             .then(subFetch => createTable(subFetch, tdV))
+//     }
+//     return btn;
+// }
+
+//myfetch
+function myfetch(url) {
+    return p1 = new Promise(function (resolve, reject) {
+        let xhr = new XMLHttpRequest();
+        xhr.open('GET', url);
+        xhr.onload = function() {
+            if(this.status == 200) {
+                resolve(JSON.parse(xhr.response));
+            } else {
+                reject (this.status);
+            }
+        };
+        xhr.send();
+    });
+}
+
+myfetch('https://swapi.dev/api/people/1/')
+    .then(luke => console.log(luke));
+
+
+//race
+function delay(ms, url) {
+    return p2 = new Promise(function (resolve, reject) {
+        let xhr = new XMLHttpRequest();
+        xhr.open('GET', url);
+        xhr.onload = function() {
+            if(this.status == 200) {
+                resolve(JSON.parse(xhr.response));
+            } else {
+                reject (this.status);
+            }
+        };
+        xhr.send();
+    });
+}
+
+delay(3000, 'https://swapi.dev/api/people/1/')
+    .then(() => console.log('выполнилось через 3 секунды'));
+
+Promise.race([p1, p2]).then((value) => {
+    console.log(value);
+});

+ 16 - 0
js_11_async_promise/style.css

@@ -0,0 +1,16 @@
+table {
+    border: 1px solid black;
+    background-color: rgba(0, 0, 0, 0.315);
+}
+
+th, td{
+    border: 1px solid black;
+}
+
+th {
+    background-color:cornflowerblue;
+}
+
+tr {
+    background-color:cornsilk
+}