fengchuanyu 8 bulan lalu
induk
melakukan
201121c2ac
4 mengubah file dengan 167 tambahan dan 19 penghapusan
  1. 22 9
      5_ES6/24_异步.html
  2. 61 10
      5_ES6/26_promise.html
  3. 54 0
      5_ES6/27_Generator.html
  4. 30 0
      5_ES6/28_async&await.html

+ 22 - 9
5_ES6/24_异步.html

@@ -19,15 +19,28 @@
         // console.log(a)
         
         //callback hell  回调地狱
-        ajaxFun("./data/a.json", function (res) {
-            console.log(res)
-            ajaxFun("./data/b.json", function (res) {
-                console.log(res)
-                ajaxFun("./data/c.json", function (res) {
-                    console.log(res)
-                })
-            })
-        })
+        // ajaxFun("./data/a.json", function (res) {
+        //     console.log(res)
+        //     ajaxFun("./data/b.json", function (res) {
+        //         console.log(res)
+        //         ajaxFun("./data/c.json", function (res) {
+        //             console.log(res)
+        //         })
+        //     })
+        // })
+
+        // event loop 事件循环
+        // 主线程任务 指同步代码
+        // 任务队列中 放置异步方法
+        // 先执行同步代码,当遇到异步方法时放到任务队列中
+        // 当同代码执行完成后 再去任务队列中查看可以执行代码
+        // 如果任务队列中有颗执行代码 在放到主线程中执行
+        // 如果异步任务执行完 内部又出现一个异步任务 再将这个内部的异步任务投放到任务队列中去
+
+        // 当有多个异步任务 异步任务分为 宏任务 微任务
+        // 微任务 promise.then (相当于vip)
+        // 宏任务 setTimeout  (普通用户)
+        // 当微任务 、宏任务同时具备颗执行状态时 优先执行为任务
 
 
 

+ 61 - 10
5_ES6/26_promise.html

@@ -1,24 +1,75 @@
 <!DOCTYPE html>
 <html lang="en">
+
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Document</title>
 </head>
+
 <body>
     <script>
-        var pro1 = new Promise(function(resolve,reject){
-            setTimeout(function(){
-                // resolve("成功了");
-                reject();
-            },1000)
-        })
+        // Promise 对象用作处理异步方法 它里面接受一个回调函数,回调函数内接受两个参数 第一个参数对应成功状态(then),第二个参数对应失败状态(cathc)
+        // var pro1 = new Promise(function(resolve,reject){
+        //     setTimeout(function(){
+        //         resolve("成功了");
+        //         // reject();
+        //     },1000)
+        // })
+
+        // pro1.then(function(res){
+        //     console.log(res);
+        // }).catch(function(){
+        //     console.log("失败了");
+        // })
+
+
+
+        // let p1 = new Promise(function (resolve, reject) {
+        //     setTimeout(function () {
+        //         resolve("p1成功")
+        //     }, 1000)
+        // })
 
-        pro1.then(function(res){
-            console.log(res);
-        }).catch(function(){
-            console.log("失败了");
+        // let p2 = new Promise(function (resolve, reject) {
+        //     setTimeout(function () {
+        //         // resolve("p2成功")
+        //         reject();
+        //     }, 2000)
+        // })
+
+        // let p3 = new Promise(function (resolve, reject) {
+        //     setTimeout(function () {
+        //         resolve("p1成功")
+        //     }, 3000)
+        // })
+
+        // Promise.all 接受一个参数 为数组 数组内部装有多个promise对象
+        // 当内部的所有promise对象都成功的时候就会进入成功态 对应 then
+        // 如果有一个失败就会进入失败状态 catch
+        // Promise.all([p1,p2,p3]).then(function(res){
+        //     console.log(res)
+        // }).catch(function(){
+        //     console.log("失败");
+        // })
+
+        // Promise.race 接受一个参数 为数组 数组内部装有多个promise对象
+        // 表示赛跑 只要有一个promise对象成功 则进入到成功态,如果第一个就成功后面就不等了;
+        // 如果全部失败 才进入失败状态 catch
+        // Promise.race([p1, p2, p3]).then(function (res) {
+        //     console.log(res)
+        // }).catch(function () {
+        //     console.log("失败");
+        // })
+
+
+        Promise.resolve().then(function(){
+            console.log("成功");
+        })
+        Promise.reject().catch(function(){
+            console.log("失败");
         })
     </script>
 </body>
+
 </html>

+ 54 - 0
5_ES6/27_Generator.html

@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <script>
+        // 使用 Generator 可讲内部代码分段执行 执行yield停止, 通过next()控制继续往下执行
+        // function* foo(){
+        //     console.log(1);
+        //     console.log("hello");
+        //     yield;
+        //     console.log(2);
+        //     yield;
+        //     console.log(3);
+        // }
+        // let foo1 = foo();
+        // foo1.next();
+        // foo1.next();
+        // foo1.next();
+
+        // next 方法有返回值 分别是当前yield语句后边的结果 第二个值为是否结束
+        // newt也可以传参数 参数给到上一个yield
+        // function* foo(){
+        //     console.log(1)
+        //     let x = yield "a";
+        //     console.log(x)
+        //     console.log(2);
+        //     yield 2;
+        //     console.log(3);
+        //     return "你好";
+        // }
+        // let foo1 = foo();
+        // console.log(foo1.next());
+        // console.log(foo1.next("hello"));
+        // console.log(foo1.next());
+
+        function* foo(x) {
+            var y = 2 * (yield (x + 1));
+            console.log(y);
+            var z = yield (y / 3);
+            return (x + y + z);
+        }
+        let foo2 = foo(1);
+        console.log(foo2.next());
+        console.log(foo2.next(3));
+        console.log(foo2.next(1));
+        
+
+    </script>
+</body>
+</html>

+ 30 - 0
5_ES6/28_async&await.html

@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+
+<body>
+    <script>
+        // 使用async 在函数前声明为异步处理函数 
+        // 在异步对象前使用await 等到异步对象执行完成之后 执行后续代码
+        // await后必须是一个promise对象
+        async function foo() {
+            console.log("1")
+            await new Promise(function(resolve,reject){
+                setTimeout(function(){
+                    console.log(2)
+                    resolve()
+                },1000)
+            })
+            console.log(3)
+        }
+
+        foo();
+    </script>
+</body>
+
+</html>