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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值