zsydgithub 1 year ago
parent
commit
795da78a17

+ 3 - 3
es6/18_Promise.html

@@ -33,12 +33,12 @@
     /* 
     /* 
       promise 有三种状态
       promise 有三种状态
         pending 进行中
         pending 进行中
-        fullfiled 已经成功
+        fufiled 已经成功
         rejected  已经失败
         rejected  已经失败
-      pending ->  fullfiled  or  pending -> rejected 
+      pending ->  fufiled  or  pending -> rejected 
       不可逆的  
       不可逆的  
       1.当一个promise 被创建的时,初始状态为pending
       1.当一个promise 被创建的时,初始状态为pending
-      2.当异步操作执行成功的时候,promise状态变为fullfiled 并且执行
+      2.当异步操作执行成功的时候,promise状态变为fufiled 并且执行
       then方法中回调函数
       then方法中回调函数
       3.当异步操作执行失败的时候,promise状态变为rejected 并且执行
       3.当异步操作执行失败的时候,promise状态变为rejected 并且执行
       catch方法中的回调函数
       catch方法中的回调函数

+ 35 - 0
es6/promise练习题2/1.html

@@ -0,0 +1,35 @@
+<!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>
+</head>
+
+<body>
+  <script>
+    const promise1 = new Promise((resolve, reject) => {
+      setTimeout(() => {
+        resolve("success");
+        console.log("timer1");
+      }, 1000);
+      console.log("promise1里的内容");
+    });
+    const promise2 = promise1.then(() => {
+      throw new Error("error!!!");
+    });
+    console.log("promise1", promise1);
+    console.log("promise2", promise2);
+    setTimeout(() => {
+      console.log("timer2");
+      console.log("promise1", promise1);
+      console.log("promise2", promise2);
+    }, 2000);        
+
+
+  </script>
+</body>
+
+</html>

+ 39 - 0
es6/promise练习题2/2.html

@@ -0,0 +1,39 @@
+<!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>
+</head>
+<body>
+  <script>
+    console.log('1')
+    setTimeout(()=>{
+      console.log('2')
+      new Promise((resolve,reject)=>{
+        console.log('3')
+        resolve()
+      }).then(()=>{
+        console.log('4')
+      })
+    },0)
+    new Promise((resolve,reject)=>{
+      console.log('5')
+      resolve()
+    }).then(()=>{
+      console.log('6')
+    })
+    setTimeout(()=>{
+      console.log('7')
+      new Promise((resolve,reject)=>{
+        console.log('8')
+        resolve()
+      }).then(()=>{
+        console.log('9')
+      })
+    },0)
+    //156   234789
+  </script>
+</body>
+</html>

+ 37 - 0
es6/promise练习题2/3.html

@@ -0,0 +1,37 @@
+<!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>
+</head>
+
+<body>
+  <script>
+    function Foo() {
+      Foo.a = function () {
+        console.log(1)
+      }
+      this.a = function () {
+        console.log(2)
+      }
+    }
+    Foo.prototype.a = function () {
+      console.log(3)
+    }
+    Foo.a = function () {
+      console.log(4)
+    }
+    Foo.a();
+    console.log(Foo)
+    let obj = new Foo();
+    console.log(obj)
+    obj.a();
+    Foo.a();
+
+  </script>
+</body>
+
+</html>

+ 37 - 0
es6/promise练习题2/4.html

@@ -0,0 +1,37 @@
+<!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>
+</head>
+
+<body>
+  <script>
+    /* finally 不管promise对象最后的状态如何 都会执行 */
+    /* finally 方法不接受任何参数 */
+
+    Promise.resolve('2')
+      .finally(() => {
+        console.log('finnnnnn')
+      })
+      .then(res => {
+        console.log('finally2后面的then函数', res)
+      })
+    Promise.resolve('1')
+      .then(res => {
+        console.log(res)
+      })
+      .finally(() => {
+        console.log('finally')
+      })
+
+
+
+
+  </script>
+</body>
+
+</html>