Skip to content

Commit a57bf66

Browse files
committed
promise
1 parent 4dfeec0 commit a57bf66

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
1.21 MB
Binary file not shown.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<script type="text/javascript">
7+
//使用Promise#then同时处理多个异步请求
8+
function getURL(url) {
9+
return new Promise(function(resolve, reject) {
10+
var req = new XMLHttpRequest();
11+
req.open('GET', url, true);
12+
req.onlaod = function() {
13+
if (req.status === 200) {
14+
resolve(req.responseText);
15+
} else {
16+
reject(new Error(req.statusText));
17+
}
18+
}
19+
req.onerror = function() {
20+
reject(new Error(req.statusText));
21+
}
22+
req.send();
23+
});
24+
}
25+
var request = {
26+
comment: function getComment() {
27+
return getURL('http://azu.github.io/promises-book/json/comment.json').then(JSON.parse);
28+
},
29+
people: function getPeople() {
30+
return getURL('http://azu.github.io/promises-book/json/people.json').then(JSON.parse);
31+
}
32+
};
33+
function main() {
34+
return Promise.all([request.comment(),request.people()]);
35+
}
36+
main().then(function(value) {
37+
console.log(value);
38+
}).catch(function(error) {
39+
console.log(error);
40+
});
41+
</script>
42+
</head>
43+
44+
<body>
45+
</body>
46+
47+
</html>

0 commit comments

Comments
 (0)