-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpromise_test.html
40 lines (35 loc) · 1.06 KB
/
promise_test.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>promise test</title>
<script src="./mypromise.js"></script>
</head>
<body>
<button id="test">promise test</button>
<div id="log"></div>
<script>
var promiseCount = 0;
function testPromise() {
var thisPromiseCount = ++promiseCount;
var log = document.getElementById('log');
log.insertAdjacentHTML('beforeend', thisPromiseCount + ') 开始(同步代码开始)');
var p1 = new Promise(
function(resolve, reject) {
log.insertAdjacentHTML('beforeend', thisPromiseCount + ') Promise开始(异步代码开始)');
window.setTimeout(function() {
resolve(thisPromiseCount);
}, Math.random() * 2000 + 1000);
}
);
p1.then(
function(val) {
log.insertAdjacentHTML('beforeend', val + ') Promise被满足了(异步代码结束)');
}
);
log.insertAdjacentHTML('beforeend', thisPromiseCount + ') 建立了Promise(同步代码结束)');
}
document.querySelector('button').addEventListener('click', testPromise);
</script>
</body>
</html>