-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathapp.js
185 lines (151 loc) · 3.8 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// console.log(1);
// setTimeout(() => {
// console.log(2);
// }, 0);
// setTimeout(() => {
// console.log(3);
// }, 0);
// setTimeout(() => {
// console.log(4);
// }, 0);
// setTimeout(() => {
// console.log(5);
// }, 0);
// setTimeout(() => {
// console.log(6);
// }, 0);
// setTimeout(() => {
// console.log(7);
// }, 0);
// console.log(8);
// function main() {
// setTimeout(() => {
// console.log('load last');
// }, 10);
// setTimeout(() => {
// console.log('load first');
// test();
// }, 0);
// test();
// }
// function test() {
// console.log('test');
// }
// main();
// Callback
/**
* 1. find user by username
* 2. find post by userId
* 3. find latest post
* 4. find comments by post id
* 5. find latest comment
* 6. find username of the latest commented user
*/
/**
* /users?username=[username]
* /posts?user_id=[user_id]
* /comments?post_id=[post_id]
* /users?username=[username]
*/
// function get(path, cb) {
// const data = {}; // somehow process it
// cb(data);
// }
// function getUserNameFromComment(username) {
// get(`users?username=${username}`, (user) => {
// get(`posts?user_id=${user.id}`, (posts) => {
// get(`comments?post_id=${posts[0].id}`, (comments) => {
// get(`users?username=${comments[0].username}`, (user) => {
// console.log(user);
// });
// });
// });
// });
// }
// getUserNameFromComment('arif');
/* const isResolved = true;
const promise = new Promise((resolve, reject) => {
if (isResolved) {
resolve('completed');
} else {
reject('data');
}
});
console.log(promise);
promise
.then((result) => {
console.log(result);
})
.catch((e) => {
console.log('Rejected');
}); */
// function wait(ms) {
// const promise = new Promise((resolve) => {
// setTimeout(resolve, ms);
// });
// return promise;
// }
// const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// wait(1000).then(() => {
// console.log('Done in 1000ms');
// });
// wait(2000).then(() => {
// console.log('Done in 2000ms');
// });
// wait(3000).then(() => {
// console.log('Done in 3000ms');
// });
// const get = (url) => Promise.resolve();
// get(`/users?username=anarul`)
// .then((user) => {
// /** do all other operations here */
// return get(`/posts?user_id=${user.id}`);
// })
// .then((posts) => {
// const latestPost = posts[0];
// return get(`/comments?post_id=${latestPost.id}`);
// })
// .then((comments) => {
// const latestComment = comments[0];
// return get(`/users?username=${latestComment.username}`);
// })
// .then((user) => {
// console.log(user);
// })
// .catch(() => {
// console.log('Error');
// });
// const get = (url) => Promise.resolve();
// async function getUserName(username) {
// try {
// const mainUser = await get(`/users?username=${username}`);
// const posts = await get(`/posts?user_id=${mainUser.id}`);
// const comments = await get(`/comments?post_id=${posts[0].id}`);
// const user = await get(`/users?username=${comments[0].username}`);
// console.log(user);
// } catch (e) {
// console.log(e);
// }
// }
const axios = require('axios').default;
const USERS_URL = 'https://jsonplaceholder.typicode.com/users';
const POSTS_URL = 'https://jsonplaceholder.typicode.com/posts';
const COMMENTS_URL = 'https://jsonplaceholder.typicode.com/comments';
async function getComments(username) {
try {
const { data: user } = await axios.get(`${USERS_URL}?username=${username}`);
const { data: posts } = await axios.get(
`${POSTS_URL}?userId=${user[0].id}`
);
const { data: comments } = await axios.get(
`${COMMENTS_URL}?postId=${posts[0].id}`
);
const { data: userWithComment } = await axios.get(
`${USERS_URL}?email=${comments[1].email}`
);
console.log(userWithComment);
} catch (error) {
console.log('Error Occurred', error.toJSON());
}
}
getComments('Bret');