-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (43 loc) · 1.3 KB
/
index.js
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
51
52
const url = "https://jsonplaceholder.typicode.com";
const getUser = async (id) => {
const res = await fetch(`${url}/users?id=${id}`);
const user = (await res.json())[0];
return user;
}
const getPosts = async (user) => {
const res = await fetch(`${url}/posts?userId=${user.id}&_limit=3`)
const posts = await res.json();
return posts;
}
const getCommentsForEachPost = async (posts) => {
const res = await Promise.all(posts.map(post =>
fetch(`${url}/comments?postId=${post.id}&_limit=2`)
))
const postComments = await Promise.all(res.map(r => r.json()));
postComments.forEach((comments, i) => posts[i].comments = comments);
}
const renderHtml = (user, posts) => {
const content = document.getElementById('content');
content.innerHTML += `<h3>Posts del usuario ${user.email}</h3>`;
posts.forEach(post => {
content.innerHTML += `
<div class="post">
<h4>${post.title}</h4>
<p>${post.body}</p>
<br>
${post.comments.map(c => `<p><span>${c.email}:</span>${c.body}</p>`).join('')}
</div>
`;
})
}
const getBlogContent = async () => {
try {
const user = await getUser(1);
const posts = await getPosts(user);
await getCommentsForEachPost(posts);
renderHtml(user, posts);
} catch (err) {
console.log(err);
}
}
getBlogContent();