-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathapp.js
156 lines (134 loc) · 3.77 KB
/
app.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// const arr = [1, 2, 3, 4];
// let index = 0;
// function next() {
// return arr[index++];
// }
// console.log(next());
// console.log(next());
// const channel = 'Stack';
// const channelIterator = channel[Symbol.iterator]();
// console.log(channelIterator.next());
// console.log(channelIterator.next());
// console.log(channelIterator.next());
// console.log(channelIterator.next());
// console.log(channelIterator.next());
// console.log(channelIterator.next());
// console.log(channelIterator.next());
// for (let v of channel) {
// console.log(v);
// }
// while (true) {
// const data = channelIterator.next();
// if (data.done) {
// break;
// }
// console.log(data.value);
// }
// const range = {
// start: 0,
// stop: 100,
// step: 5,
// };
// range[Symbol.iterator] = function () {
// let current = this.start;
// const stop = this.stop;
// const step = this.step;
// return {
// next() {
// const o = {
// value: current,
// done: current > stop,
// };
// current += step;
// return o;
// },
// };
// };
// for (let v of range) {
// console.log(v);
// }
// const rangeIterator = range[Symbol.iterator]();
// console.log(rangeIterator.next());
// console.log(rangeIterator.next());
// console.log(rangeIterator.next());
// function* myGenerator() {
// yield 1;
// yield 2;
// yield 3;
// }
// const iterator = myGenerator();
// console.log(iterator.next());
// console.log(iterator.next());
// console.log(iterator.next());
// console.log(iterator.next());
// Generator always returns iterator
// function* range(start = 0, stop = 100, step = 5) {
// for (let i = start; i <= stop; i += step) {
// yield i;
// }
// }
// const rangeIt = range(1, 10, 3);
// console.log(rangeIt.next());
// console.log(rangeIt.next());
// console.log(rangeIt.next());
// console.log(rangeIt.next());
// console.log(rangeIt.next());
// console.log(rangeIt.next());
// console.log(rangeIt.next());
// console.log(rangeIt.next());
// console.log(rangeIt.next());
// for (let v of range()) {
// console.log(v);
// }
// function* generateId() {
// let index = 1;
// while (true) {
// yield index++;
// }
// }
// const generateUserId = generateId();
// const generateProductId = generateId();
// console.log('User', generateUserId.next().value);
// console.log('User', generateUserId.next().value);
// console.log('User', generateUserId.next().value);
// console.log('Product', generateProductId.next().value);
// console.log('Product', generateProductId.next().value);
// console.log('Product', generateProductId.next().value);
// console.log('Product', generateProductId.next().value);
// console.log('Product', generateProductId.next().value);
// console.log('Product', generateProductId.next().value);
const axios = require('axios').default;
async function getUsers() {
const url = 'https://jsonplaceholder.typicode.com/users';
const { data: users } = await axios.get(url);
return users.map((user) =>
axios.get(`https://jsonplaceholder.typicode.com/posts?userId=${user.id}`)
);
}
// async function* getPostsByUser(users) {
// const url = 'https://jsonplaceholder.typicode.com/posts';
// for (let i = 0; i < users.length; i++) {
// const { data: posts } = await axios.get(`${url}?userId=${users[i].id}`);
// yield posts;
// }
// }
// getUsers()
// .then(async (users) => {
// // const userIterator = await getPostsByUser(users);
// // await userIterator.next();
// // await userIterator.next();
// // console.log((await userIterator.next()).value);
// // for await (let v of getPostsByUser(users)) {
// // console.log(v.map((d) => d.title));
// // }
// console.log(users);
// })
// .catch((e) => {
// console.log(e);
// });
(async () => {
const users = await getUsers();
for await (let v of users) {
console.log(v.data.map((post) => post.title));
}
})();