-
Notifications
You must be signed in to change notification settings - Fork 0
/
promises.js
115 lines (96 loc) · 3.21 KB
/
promises.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
// Promise is a JavaScript object for asynchronous operation.
// Promises returns an object to which you attach callbacks, instead of passing callbacks into a function.
// Promises are easier to read, easier to write, easier to debug.
// Promises avoid callback hell.
// Promises status:
// -- pending: operation is pending
// -- fulfilled : operation is completed
// -- rejected: operation is failed
const promise = new Promise((resolve, reject) => {
// doing some heavy work (network, read files)
let connect = true;
if (connect) {
resolve("You are connected!");
} else {
reject(new Error("No network available!"));
}
});
// console.log(promise); // pending
// callback function to be executed when the promise is resolved
const onResolve = (value) => {
console.log(value);
};
// callback function to be executed when the promise is rejected
const onReject = (error) => {
console.error(error);
};
// then(): Attaches callbacks for the resolution and/or rejection of the Promise.
promise.then(onResolve, onReject);
// =======================================================================
// Avoid the callback hell problem using promises:
// =======================================================================
function getUserData(useId) {
return new Promise((resolve, reject) => {
setTimeout(() => {
const userData = { id: userId, name: "John Doe", age: 30 };
resolve(userData);
}, 1000);
});
}
function getUserPosts(userId) {
return new Promise((resolve, reject) => {
setTimeout(() => {
const userPosts = [
{ id: 1, title: "Post 1", body: "Body of post 1" },
{ id: 2, title: "Post 2", body: "Body of post 2" },
{ id: 3, title: "Post 3", body: "Body of post 3" },
];
resolve(userPosts);
}, 2000);
});
}
function getUserComments(userId) {
return new Promise((resolve, reject) => {
setTimeout(() => {
const userComments = [
{ id: 1, postId: 1, text: "Comment 1 for post 1" },
{ id: 2, postId: 1, text: "Comment 2 for post 1" },
{ id: 3, postId: 2, text: "Comment 1 for post 2" },
{ id: 4, postId: 2, text: "Comment 2 for post 2" },
{ id: 5, postId: 3, text: "Comment 1 for post 3" },
];
resolve(userComments);
}, 3000);
});
}
function getUserReactions(userId) {
return new Promise((resolve, reject) => {
setTimeout(() => {
const userReactions = [
{ id: 1, postId: 1, text: "Reaction 1 for post 1" },
{ id: 2, postId: 1, text: "Reaction 2 for post 1" },
{ id: 3, postId: 2, text: "Reaction 1 for post 2" },
{ id: 4, postId: 2, text: "Reaction 2 for post 2" },
{ id: 5, postId: 3, text: "Reaction 1 for post 3" },
];
resolve(userReactions);
}, 4000);
});
}
const userId = 123;
getUserData(userId)
.then((userData) => {
console.log("user Data: ", userData);
return getUserPosts(userId);
})
.then((userPosts) => {
console.log("user posts: ", userPosts);
return getUserComments(userId);
})
.then((userComments) => {
console.log("user comments: ", userComments);
return getUserReactions(userId);
})
.then((userReactions) => {
console.log("user Reactions: ", userReactions);
});