forked from HackYourFuture/JavaScript3
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexercise2.js
More file actions
50 lines (35 loc) · 1 KB
/
Copy pathexercise2.js
File metadata and controls
50 lines (35 loc) · 1 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
const urls = [
'https://jsonplaceholder.typicode.com/todos/1',
'https://jsonplaceholder.typicode.com/todos/2',
'https://jsonplaceholder.typicode.com/todos/3',
'https://jsonplaceholder.typicode.com/todos/4',
'https://jsonplaceholder.typicode.com/todos/5',
]
// BASIC VERSION:
// Start off with a promise that always resolves
var sequence = Promise.resolve();
// Loop through our chapter urls
story.chapterUrls.forEach((chapterUrl) => {
// Add these actions to the end of the sequence
sequence = sequence.then(() => {
return getJSON(chapterUrl);
}).then((chapter) => {
addHtmlToPage(chapter.html);
});
})
// =============================== //
// CLEAN VERSION:
urls.reduce(
(sequence, chapterUrl) => {
// Add these actions to the end of the sequence
return sequence.then(() => {
return getJSON(chapterUrl);
}).then((chapter) => {
addHtmlToPage(chapter.html);
});
},
Promise.resolve()
);
function getJSON(url) {
return fetch(url).then(JSON.parse);
}