Browse Source

promise done

pocu46 4 years ago
parent
commit
4d4e53496b
4 changed files with 134 additions and 6 deletions
  1. 10 2
      13 promise/index.html
  2. 101 1
      13 promise/script.js
  3. 1 1
      13 promise/style.css
  4. 22 2
      React App ()/some-project/src/App.js

+ 10 - 2
13 promise/index.html

@@ -11,8 +11,16 @@
 
     <body>
         <div class="wrapper">
-            <div class="div1">1</div>
-            <div class="div2">2</div>
+            <div class="div1" id="table1"></div>
+            <div class="div2" id="table2"></div>
+
+            <div>
+                <input type="number" id="num">
+                <button id="btn">
+                    Race
+                </button>
+                <input type="text" id="race">
+            </div>
         </div>
 
         <script src="./script.js"></script>

+ 101 - 1
13 promise/script.js

@@ -1,3 +1,103 @@
+//-------------------------------------------------fetch basic-------------------------------------------------------------------------
+
 // fetch('https://swapi.dev/api/people/1/')
 //   .then(res => res.json())
-//   .then(luke => console.log(luke))
+//   .then(luke => console.log(luke))
+
+function pushToTable(element, name) {
+    const url = "http://swapi.dev/api/";
+
+    function addButton(elem, value) {
+        let button = document.createElement("button");
+        button.append(value);
+        elem.append(button);
+
+        button.onclick = () => {
+            table2.removeChild(table2.firstChild);
+            fetch(value)
+                .then((res) => res.json())
+                .then((luke) => pushToTable(table2, luke));
+        }
+    }
+
+    let table = document.createElement("table");
+    table.setAttribute("border", "2");
+    element.append(table);
+    for (let key in name) {
+        let tr = document.createElement("tr");
+        table.append(tr);
+
+        let th = document.createElement("th");
+        tr.append(th);
+        th.append(key);
+
+        let td = document.createElement("td");
+        tr.append(td);
+
+        if (typeof name[key] == "string") {
+            if (name[key].startsWith(url)) {
+                addButton(td, name[key]);
+            } else {
+                td.append(name[key])
+            }
+        } else {
+            if (Array.isArray(name[key])) {
+                for (let key2 of name[key]) {
+                    if (key2.startsWith(url)) {
+                        addButton(td, key2)
+                    } else {
+                        td.append(key2)
+                    }
+                }
+            }
+        }
+    }
+}
+
+fetch("https://swapi.dev/api/people/1/")
+    .then((res) => res.json())
+    .then((luke) => {
+        pushToTable(table1, luke);
+        pushToTable(table2, luke);
+    });
+
+//-------------------------------------------------myfetch-------------------------------------------------------------------------
+
+let myfetch = function (url) {
+    return new Promise(function (resolve, rejected) {
+        let request = new XMLHttpRequest();
+        request.open("GET", url, true);
+        request.responseType = "json";
+
+        request.onload = function () {
+            if (this.status == 200) {
+                resolve(this.response)
+            } else {
+                rejected(new Error(`Error: ${this.status}: ${this.statusText}`))
+            }
+        }
+
+        request.send(null)
+    })
+}
+
+myfetch("https://swapi.dev/api/people/1/").then(
+    (luke) => console.log(JSON.stringify(luke)),
+    (error) => alert(error)
+);
+
+//-------------------------------------------------race-------------------------------------------------------------------------
+
+btn.onclick = function () {
+    function delay(milliSecondsnds) {
+        return new Promise((resolved) => setTimeout(() => resolved(`Delay ${milliSecondsnds}`), milliSecondsnds))
+    }
+    Promise.race([delay(+num.value),
+        myfetch("https://swapi.dev/api/people/1/")
+    ]).then((resolved) => {
+        race.value = resolved;
+        console.log(resolved);
+    })
+}
+
+const num = document.getElementById("num");

+ 1 - 1
13 promise/style.css

@@ -4,7 +4,7 @@
 }
 
 .wrapper {
-    display: inline inline-flex;
+    display: flex;
 }
 
 .div1 {

+ 22 - 2
React App ()/some-project/src/App.js

@@ -1,4 +1,6 @@
-import React, {useState} from 'react'
+import { render } from '@testing-library/react';
+import React, {useState} from 'react';
+import {BrowserRouter as Router, Switch, Route, Link} from 'react-router-dom';
 
 const LoginFrom = () => {
    const [login, setLogin] = useState('')
@@ -14,9 +16,27 @@ const LoginFrom = () => {
     )
 }
 
+// const Acc = ({param: {a, b}}) => {
+const Acc = (props) => {
+    return(
+        <>
+            <div>{console.log(props)}</div>
+        </>
+    )
+}
+
 function App() {
     return (
-        <LoginFrom />
+        <>
+            <LoginFrom />           
+            <Router>
+                <Switch>
+                        {/* <Route exact path="/" component={Home} /> */}
+                        <Route exact path="/acc/:_id" component={Acc} />
+                        {/* <Route exact path="/top" component={Top} /> */}
+                </Switch>
+            </Router>
+        </>
     );
 }