Vitalii Polishchuk hace 3 años
padre
commit
ec173f1312

+ 44 - 0
js/12-js-async-promise/css/main.css

@@ -0,0 +1,44 @@
+table {
+    font-size: 14px;
+    text-align: center;
+    border: 1px solid #9c9c9c;
+    margin: 20px;
+}
+
+td:first-child {
+    background: #3f96e2;
+    color: white;
+    padding: 10px;
+}
+
+td {
+    border-style: solid;
+    border-width: 0 1px 1px 0;
+    border-color: white;
+    background: #d8e6f3;
+}
+
+input[type="button"] {
+    border: none;
+    color: white;
+    padding: 5px 15px;
+    text-decoration: none;
+    cursor: pointer;
+    margin: 5px;
+    background-color: #afcde7;
+    border-radius: 10px;
+    transition: all 0.5s;
+}
+
+input[type="button"]:hover {
+    background-color: #64a7e2;
+    border-radius: 10px;
+    transform: translate(-2px, -2px);
+    transition: all 0.5s;
+}
+
+input[type="button"]:active {
+    box-shadow: none;
+    transform: translate(0, 0);
+    transition: all 0.5s;
+}

+ 17 - 0
js/12-js-async-promise/index.html

@@ -0,0 +1,17 @@
+<!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">
+    <title>Document</title>
+    <link rel="stylesheet" href="css/main.css">
+</head>
+
+<body>
+
+    <script src="js/main.js"></script>
+</body>
+
+</html>

+ 108 - 0
js/12-js-async-promise/js/main.js

@@ -0,0 +1,108 @@
+//fetch basic
+fetch('https://swapi.dev/api/people/1/')
+    .then(res => res.json())
+    .then((data, parent = document.body) => {
+        let table = document.createElement("table");
+        for (let key in data) {
+            let tr = document.createElement("tr");
+            let td1 = document.createElement("td");
+            let td2 = document.createElement("td");
+
+            td1.innerText = key;
+            td2.innerText = data[key];
+
+            tr.append(td1);
+            tr.append(td2)
+
+            table.append(tr)
+        }
+        parent.append(table)
+    }
+    )
+
+//fetch improved
+let showData = (data, parent = document.body) => {
+    let table = document.createElement("table");
+
+    for (let key in data) {
+        let tr = document.createElement("tr");
+        let td1 = document.createElement("td");
+        let td2 = document.createElement("td");
+
+        td1.innerText = key;
+
+        if (Array.isArray(data[key]) && (data[key][0] !== undefined && data[key][0].includes("https://swapi.dev/api/"))) {
+            for (let j of data[key]) {
+                let btn = document.createElement("input");
+                btn.type = "button";
+                btn.value = "LOOK";
+                td2.append(btn)
+
+                btn.onclick = e => {
+                    table.remove();
+                    fetch(j)
+                        .then(res => res.json())
+                        .then(json => showData(json))
+                }
+            }
+        } else if (typeof data[key] === "string" && data[key].includes("https://swapi.dev/api/")) {
+            let btn = document.createElement("input");
+            btn.type = "button";
+            btn.value = "LOOK";
+            td2.append(btn)
+
+            btn.onclick = e => {
+                table.remove()
+                fetch(data[key])
+                    .then(res => res.json())
+                    .then(json => showData(json))
+            }
+        } else {
+            td2.innerText = data[key];
+        }
+        tr.append(td1);
+        tr.append(td2)
+        table.append(tr)
+    }
+    parent.append(table)
+}
+
+fetch('https://swapi.dev/api/people/1/')
+    .then(res => res.json())
+    .then(json => showData(json))
+
+//myfetch
+let myfetch = url => {
+    return new Promise((resolve, reject) => {
+        const xhr = new XMLHttpRequest();
+
+        xhr.open("GET", url, true)
+        xhr.send()
+
+        xhr.onreadystatechange = () => {
+            if (xhr.readyState != 4) return;
+
+            if (xhr.status != 200) {
+                let error = new Error(xhr.statusText);
+                error.code = xhr.status;
+                reject(error);
+            } else {
+                resolve(JSON.parse(xhr.responseText));
+            }
+        }
+    })
+}
+
+myfetch('https://swapi.dev/api/people/1/')
+    .then(json => showData(json))
+
+//race
+function delay(ms) {
+    function executor(ok, fail) {
+        setTimeout(() => ok(ms), ms)
+    }
+    let p = new Promise(executor)
+    return p
+}
+
+Promise.race([myfetch('https://swapi.dev/api/people/1/'), delay(268)]).then(first => console.log(first))