-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (44 loc) · 1.31 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
getUser(1)
.then(user => getRepositories(user.gitHubUsername))
.then(repos => getCommits(repos[0]))
.then(commits => console.log('Commits', commits))
.catch(err => console.log('Error', err.message));
console.log('Before');
async function displayCommits() {
try {
const user = await getUser(1);
const repos = await getRepositories(user.gitHubUsername);
const commit = await getCommits(repos[0]);
console.log(commit);
}
catch (err) {
console.log('Error', err.message);
}
}
displayCommits()
console.log('After');
function getUser(id) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Reading a user from database...')
resolve({ id: id, gitHubUsername: 'test' });
}, 2000);
});
}
function getRepositories(username) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Calling GitHub API...')
resolve(['repo1', 'repo2', 'repo3']);
reject(new Error('Could not get the repos.'));
}, 2000);
});
}
function getCommits(repo) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Calling GitHub API...');
resolve(['commit']);
}, 2000);
});
}