-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodos.js
40 lines (34 loc) · 1.22 KB
/
todos.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
const fetch = require("node-fetch");
const usersUri = "https://jsonplaceholder.typicode.com/users";
const todosUri = "https://jsonplaceholder.typicode.com/todos";
function getUsersWithTodosUsingPromises() {
return Promise.all([fetch(usersUri), fetch(todosUri)])
.then(responses => {
return Promise.all(responses.map(response => response.json()));
})
.then(([users, todos]) => {
return users.map(user => ({
name: user.name,
todos: todos
.filter(todo => todo.userId === user.id)
.map(todo => ({ title: todo.title, done: todo.done }))
}));
});
}
const runAsync = require("./runAsync");
function* getUsersWithTodos() {
const responses = yield Promise.all([fetch(usersUri), fetch(todosUri)]);
const [users, todos] = yield Promise.all(
responses.map(response => response.json())
);
const usersWithTodos = users.map(user => ({
name: user.name,
todos: todos
.filter(todo => todo.userId === user.id)
.map(todo => ({ title: todo.title, done: todo.done }))
}));
console.log(JSON.stringify(usersWithTodos, null, 2));
return usersWithTodos;
}
runAsync(getUsersWithTodos);
module.exports = { getUsersWithTodos, getUsersWithTodosUsingPromises };