-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_promise_chaining_init.js
More file actions
31 lines (28 loc) · 1.53 KB
/
1_promise_chaining_init.js
File metadata and controls
31 lines (28 loc) · 1.53 KB
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
const p = new Promise((resolve, reject) => {
/* Notice how the promise is immediately resolved, so its fulfilled, with a result == 1 */
resolve(1);
})
/* First Promise Raction RETURNS a new result, so CREATES ANOTHER PROMISE, RESOLVED, with result == 2, basically .then() returns a promise, so if you return a value, it will be wrapped in a Promise.resolve(value) */
.then(result => {console.log(result); return result * 2})
/* Also RETURNS a new result, so CREATES ANOTHER PROMISE, RESOLVED, with result == 4 */
.then(result => {console.log(result); return result * 2})
/* Last promise does not return, so it has an undefined result, but the promise object itself is fulfilled so we see logged 4 */
.then(result => console.log(result))
console.log("FINALLY")
/* since everything is queued in the microtask queue BUT the last log, we should see: FINALLY, 1, 2, 4 */
/**
* This Promise chaining is useful in multi-chained async scenarios, let's take the example of fetching some user data, that we need to go through until get to a specific comment of one of his posts :
* (that means multiple chained async backend call: getUser -> getUserPosts -> getUserComments)
* fetch("https://mainAPIUrl:443/getUser/?user=myUser")
* .then((user) => {
* console.log("User Id:", user.id);
* return fetchUserPosts(user.id);
* })
* .then((posts) => {
* console.log("User Posts:", posts);
* return fetchPostComments(posts[0].id);
* })
* .then((comments) => {
* console.log("User comments:", comments);
* })
*/