forked from googlesamples/web-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-all-example.html
41 lines (40 loc) · 1.23 KB
/
async-all-example.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
<!DOCTYPE html>
<html>
<head>
<title>Promises test</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="network-fake">
<label><input type="checkbox"> Fake network delay</label>
</div>
<div class="story"></div>
<svg class="spinner" viewBox="0 0 100 100" width="20" height="20">
<circle cx="50" cy="50" r="42" transform="rotate(-90,50,50)" />
</svg>
<script src="utils.js"></script>
<script>
getJson('story.json').then(function(story) {
addHtmlToPage(story.heading);
// Take an array of promises and wait on them all
return Promise.all(
// Map our array of chapter urls
// to an array of chapter json promises
story.chapterUrls.map(getJson)
);
}).then(function(chapters) {
// Now we have the chapters jsons in order! Loop thorugh…
chapters.forEach(function(chapter) {
// …and add to the page
addHtmlToPage(chapter.html);
});
addTextToPage("All done");
}).catch(function(err) {
// catch any error that happened along the way
addTextToPage("Argh, broken: " + err.message);
}).then(function() {
document.querySelector('.spinner').style.display = 'none';
});
</script>
</body>
</html>