You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: answers/chaining-promises.js
+47Lines changed: 47 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,54 @@ function chainTwoAsyncProcesses(firstPromise, slowAsyncProcess){
35
35
returnfirstPromise.then(/* IMPLEMENT ME! */);
36
36
}
37
37
38
+
/**
39
+
*
40
+
* EXERCISE 3
41
+
*
42
+
* The most common point of this scenario is to chain two async processes, especially when the some result of the first one is needed before we can start the second. This is a very common technique, and we're going to use it to implement a somewhat more "real-world" scenario.
43
+
*
44
+
* Our situation is that we have two api calls, one that gets a user object by id, and one that gets an organization object by id. The user object might look like this:
45
+
*
46
+
* {
47
+
* id: 'u001',
48
+
* name: 'Mike',
49
+
* email: 'mike@mike.mike',
50
+
* organizationId: 'o001'
51
+
* }
52
+
*
53
+
* The organization object might look like this:
54
+
*
55
+
* {
56
+
* id: 'o001',
57
+
* name: 'Research and Development'
58
+
* }
59
+
*
60
+
* We want to make a function that combines these two calls into one, making an object that looks like the following:
61
+
*
62
+
* {
63
+
* id: 'u001',
64
+
* name: 'Mike',
65
+
* email: 'mike@mike.mike',
66
+
* organizationId: 'o001'
67
+
* organization: {
68
+
* id: 'o001',
69
+
* name: 'Research and Development'
70
+
* }
71
+
* }
72
+
*
73
+
* We can't get the organization object until we have the user object and the organization id. That means that we have to request the user, wait until we have it, then request the organization, wait for it to return, and return a combined object.
74
+
*
75
+
* It's also worth noting that the getUserById function will resolve with undefined if no user is found with that id. Then we don't have to request the organization at all!
76
+
*
77
+
* For this example, the test creates the getUserById and getOrganizationById functions, then passes them to a function that itself returns the function we want to build: one which takes in a userId and returns a combined object.
0 commit comments