Promise状态改变与触发调用的记录

 1 <html>
 2 
 3 <head>
 4     <title>Parcel Sandbox</title>
 5     <meta charset="UTF-8" />
 6     <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
 7 </head>
 8 
 9 <body>
10     <button id='btn'>Get Data</button>
11 
12 <script>
13     function showError(e) {
14         console.warn("Error", e);
15     }
16 
17     function fail() {
18         console.log("fail", arguments);
19     }
20 
21     function success() {
22         console.log("success", arguments);
23     }
24 
25     function getUser(id) {
26         return new Promise((a, b) => {
27             if (id % 2 == 0) {
28                 a(654321);
29             } else {
30                 b(123456);
31             }
32         });
33     }
34 
35     $("#btn").on("click", () => {
36         getUser(666)
37         //当then的参数为两个的时候 无论如何都不执行catch
38         .then(success, fail)
39         //.finally(success)
40         //当then没有第二个参数的时候 b会触发一个找不到函数的异常 被catch捕获
41         .catch(showError);
42     });
43 </script>
44 </body>
45 
46 </html>
原文地址:https://www.cnblogs.com/toumingbai/p/11445779.html