-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
56 lines (47 loc) · 1.16 KB
/
promise.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
const username = process.argv[2];
const {
fetchUser,
fetchRepos,
fetchGists,
} = require('./gh-requests');
let result = [];
fetchUser(username)
.then(userRes => userRes.json())
.then((user) => {
if (user.message === 'Not Found') throw new Error('User Not Found');
result = result.concat(user);
return fetchRepos(username);
})
.then(reposRes => reposRes.json())
.then((repos) => {
result = result.concat(repos);
return fetchGists(username);
})
.then(gistsRes => gistsRes.json())
.then((gists) => {
result = result.concat(gists);
return result;
})
.then(combinedResult => console.log(combinedResult))
.catch(error => console.log(error))
// nested promises *bad*
fetchUser(username)
.then(userRes => userRes.json())
.then((user) => {
if (user.message === 'Not Found') throw new Error(user.message);
return fetchRepos(username)
.then(reposRes => reposRes.json())
.then(repos => {
return fetchGists(username)
.then(gistsRes => gistsRes.json())
.then((gists) => {
return [
user,
repos,
gists,
];
})
})
})
.then(combindedResult => console.log(combindedResult))
.catch(error => console.log(error))