-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.js
More file actions
45 lines (43 loc) · 1.28 KB
/
client.js
File metadata and controls
45 lines (43 loc) · 1.28 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
import { addPost } from '/builder.js';
export function getAllPosts() {
const thePromises = [];
for (let index = 1; index <= 100; index++) {
thePromises.push(
fetch(`https://jsonplaceholder.typicode.com/posts/${index}`)
);
}
Promise.all(thePromises).then(postValues => {
const allPosts = postValues.map(postResponse => postResponse.json());
Promise.all(allPosts)
.then(postObjects => {
const authorResponses =
postObjects.map(post => getAuthor(post.userId));
return Promise.all(authorResponses)
.then(authorObjects => {
return postObjects.map((post, index) => {
post.author = authorObjects[index];
return post
})
})
})
.then(allOrderedPosts => {
return allOrderedPosts.sort((left, right) => {
return left.id > right.id ? -1 : 1;
});
})
.then(reversePostData => {
console.log("All the posts...");
console.log({ allPostData: reversePostData });
reversePostData.forEach(post => {
addPost(post);
});
});
});
}
function getAuthor(authorId) {
return fetch(`https://jsonplaceholder.typicode.com/users/${authorId}`).then(
response => {
return response.json();
}
);
}