promise.html
1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<html>
<head>
</head>
<body>
<div style="display:none">
<script>
'use strict';
let score = 0;
console.log("score 1",score);
var PromiseObj = new Promise(function(resolve,reject){
score = 1;
console.log("score 2",score);
resolve('booooooooooooooooo')
});
PromiseObj.then((A)=>{
console.log("A",A);
console.log("score 3",score);
score += 20;
return 12
}).then((AA)=>{
console.log("AA",AA,score);
}).then(
score = 0,
).then((B)=>{
console.log("B",B);
console.log("score 5",score);
return Promise.resolve().then(()=>{
console.log("score 6",score);
const C = score;
console.log("c",C);
if(score > 0){
throw new Error('zhong');
}
})
}).catch((D)=>{
console.log("score 7",score);
console.log("d",D);
})
<!-- function runAsync(){ -->
<!-- var p = new Promise(function(resolve, reject){ -->
<!-- //做一些异步操作 -->
<!-- setTimeout(function(){ -->
<!-- console.log('执行完成'); -->
<!-- resolve('随便什么数据'); -->
<!-- }, 2000); -->
<!-- }); -->
<!-- return p; -->
<!-- } -->
<!-- runAsync().then(function(data){ -->
<!-- console.log(data); -->
<!-- //后面可以用传过来的数据做些其他操作 -->
<!-- //...... -->
<!-- }); -->
</script>
</div>
</body>
</html>