-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.js
61 lines (50 loc) · 1.2 KB
/
async.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
// const promise = new Promise((resolve, reject) => {
// setTimeout(() => {
// // resolve("Success");
// reject(new Error("Not logged in"));
// }, 2000);
// });
// promise
// .then(user => {
// console.log(user);
// })
// .catch(err => {
// console.log(err.message);
// });
// n-th callback will become callback-hell
console.log("Start");
function loginUser(email, password) {
return new Promise(resolve => {
resolve({ userEmail: email })
}, 3000);
}
function getUserVideos(email) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(email === "Eka") {
resolve(["video 1", "video 2"]);
} else {
reject("No email / username");
}
}, 2000);
});
}
function getVideoDetails(video) {
return new Promise(resolve => {
setTimeout(() => {
console.log("Details video");
}, 1000);
});
}
async function showDisplay() {
try {
const loggedUser = await loginUser("Eka", 123);
const videos = await getUserVideos(loggedUser.userEmail);
console.log(videos);
const detail = await getVideoDetails(videos);
} catch (error) {
console.log("Can not get user");
}
}
showDisplay();
console.log("Finish");