From f0837dbfdfc2e677928d67b4fbc0410d3f79f126 Mon Sep 17 00:00:00 2001
From: kangkai <kangk26@foxmail.com>
Date: Wed, 07 Apr 2021 19:49:09 +0800
Subject: [PATCH] Promise约定与链式调用

---
 Promise.html |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/Promise.html b/Promise.html
index e69de29..a8fe8fe 100644
--- a/Promise.html
+++ b/Promise.html
@@ -0,0 +1,57 @@
+<html>
+    <head>        
+        <title>Promise</title>
+        <script type='text/javascript'>
+            var ok = true;
+            function successCallBack(){
+                console.log("成功");
+            };
+            function failureCallBack(){
+                console.log("失败");
+            };
+            function successCallBack1(){
+                console.log("成功1");
+                return new Promise((resolve,reject) => {
+                    reject();
+                })
+            };
+            function failureCallBack1(){
+                console.log("失败1");
+            };
+            function successCallBack2(){
+                console.log("成功2");
+            };
+            function failureCallBack2(){
+                console.log("失败2");
+            };
+
+            const wait = ()=> new Promise((resolve,reject)=> {
+                setTimeout(() => {
+                    console.log('setTimeout执行完毕');
+                    resolve();
+                }, 0);                                
+            });       
+                        
+            const wait2 = ()=> new Promise((resolve,reject)=> {
+                resolve();
+                setTimeout(() => {
+                    console.log('setTimeout执行完毕');                    
+                }, 1000);                                
+            });     
+
+            //Promise约定与链式调用
+            //1.在本轮 事件循环 运行完成之前,回调函数是不会被调用的。以下是个错误示例,resolve回调方法要在setTimeout执行完后在调用。
+            var use = wait2().then(successCallBack,failureCallBack);
+            //2.即使异步操作已经完成(成功或失败),在这之后通过 then() 添加的回调函数也会被调用。
+            var use2 = wait();
+            setTimeout(() => {
+                use.then(successCallBack,failureCallBack);    
+            }, 2000);                        
+            //3.通过多次调用 then() 可以添加多个回调函数,它们会按照插入顺序进行执行。一般情况下then会返回一个默认的Promise对象。
+            var use3 = wait().then(successCallBack,failureCallBack).then(successCallBack1,failureCallBack1).then(successCallBack2,failureCallBack2);
+        </script>
+    </head>
+    <body>
+
+    </body>
+</html>
\ No newline at end of file

--
Gitblit v1.9.3