forked from RubyLouvre/mmDeferred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
then3.html
69 lines (61 loc) · 2.07 KB
/
then3.html
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
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="deferred.js"></script>
<script>
window.onload = function() {
var oldConsole = window.console
window.console = {
log: function(str) {
if (oldConsole) {
oldConsole.log(str)
}
var div = document.createElement("div")
div.innerHTML = str;
document.body.appendChild(div);
}
}
function checkRule(b, dd) {
if (b) {
dd.resolve(true)
} else {
dd.reject("error")
}
}
function aaa() {
var d = Deferred();
setTimeout(function() {
d.resolve(1000)
}, 1000)
return d.promise
}
var promise = aaa().then(function(e) {
var dd = Deferred();
checkRule(1, dd)
console.log("会经过这里")
return dd.promise
})
promise = promise.then(function(e) {
var dd = Deferred();
console.log("也会经过这里")
checkRule(0, dd)
return dd.promise
}).then(function(e) {
var dd = Deferred();
console.log("这里不会执行")
checkRule(1, dd)
return dd.promise
})
//分开
promise.otherwise(function(e) {
console.log("最后到达这里 " + e)
})
}
</script>
</head>
<body>
<div>TEST by 司徒正美</div>
</body>
</html>