Skip to content

Commit baf560b

Browse files
author
JoelCodes
committed
Adds text for users scenario
1 parent 29d5fbf commit baf560b

File tree

2 files changed

+98
-51
lines changed

2 files changed

+98
-51
lines changed

answers/chaining-promises.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,54 @@ function chainTwoAsyncProcesses(firstPromise, slowAsyncProcess){
3535
return firstPromise.then(/* IMPLEMENT ME! */);
3636
}
3737

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.
78+
*
79+
* @param {function} getUserById
80+
* @param {function} getOrganizationById
81+
*/
3882
function makeGetUserByIdWithOrganization(getUserById, getOrganizationById){
83+
return function getUserByIdWithOrganization(userId){
84+
/* IMPLEMENT ME! */
85+
};
3986
}
4087

4188
module.exports = {

test/chaining-promises.test.js

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,64 @@ const {
88
} = require('../answers/chaining-promises.js');
99

1010
describe('Chaining Promises with .then(cb) and .catch(cb)', () => {
11-
// describe('#flatMapPromise(promise, asyncTransformer) => Promise', () => {
12-
// context('If the first promise resolves', () => {
13-
// const firstPromise = Promise.resolve(3);
14-
// it('resolves with the value of the second promise', () => {
15-
// const resolveAndSquare = (val) => Promise.resolve(val * val);
16-
// return flatMapPromise(firstPromise, resolveAndSquare)
17-
// .then((val) => {
18-
// assert.equal(val, 9);
19-
// });
20-
// });
21-
// it('rejects with the error of the second promise', () => {
22-
// const freakOut = (val) => Promise.reject(`Boo! ${val}`);
23-
// return flatMapPromise(firstPromise, freakOut)
24-
// .then((val) => {
25-
// assert.fail(`This should not have resolved! It resolved with ${val}`);
26-
// }, (err) => {
27-
// assert.equal(err, 'Boo! 3');
28-
// });
29-
// });
30-
// });
31-
// context('If the first promise rejects', () => {
32-
// it('rejects with the error of the first promise', () => {
33-
// return flatMapPromise(Promise.reject('Boo!'), (val) => Promise.reject(val))
34-
// .then((val) => {
35-
// assert.fail(`This should not have resolved! It resolved with ${val}`);
36-
// }, (err) => {
37-
// assert.equal(err, 'Boo!');
38-
// });
39-
// });
40-
// });
41-
// });
11+
describe('#flatMapPromise(promise, asyncTransformer) => Promise', () => {
12+
context('If the first promise resolves', () => {
13+
const firstPromise = Promise.resolve(3);
14+
it('resolves with the value of the second promise', () => {
15+
const resolveAndSquare = (val) => Promise.resolve(val * val);
16+
return flatMapPromise(firstPromise, resolveAndSquare)
17+
.then((val) => {
18+
assert.equal(val, 9);
19+
});
20+
});
21+
it('rejects with the error of the second promise', () => {
22+
const freakOut = (val) => Promise.reject(`Boo! ${val}`);
23+
return flatMapPromise(firstPromise, freakOut)
24+
.then((val) => {
25+
assert.fail(`This should not have resolved! It resolved with ${val}`);
26+
}, (err) => {
27+
assert.equal(err, 'Boo! 3');
28+
});
29+
});
30+
});
31+
context('If the first promise rejects', () => {
32+
it('rejects with the error of the first promise', () => {
33+
return flatMapPromise(Promise.reject('Boo!'), (val) => Promise.reject(val))
34+
.then((val) => {
35+
assert.fail(`This should not have resolved! It resolved with ${val}`);
36+
}, (err) => {
37+
assert.equal(err, 'Boo!');
38+
});
39+
});
40+
});
41+
});
4242

43-
// describe('#chainTwoAsyncProcesses(firstPromise, slowAsyncProcess)', () => {
44-
// it('runs a slow process on the result of the numberPromise', () => {
45-
// const time = new Date();
46-
// const numberPromise = Promise.resolve(31);
43+
describe('#chainTwoAsyncProcesses(firstPromise, slowAsyncProcess)', () => {
44+
it('runs a slow process on the result of the numberPromise', () => {
45+
const time = new Date();
46+
const numberPromise = Promise.resolve(31);
4747

48-
// function slowSquarer(num){
49-
// return new Promise((resolve) => {
50-
// setTimeout(() => {
51-
// resolve(num * num);
52-
// }, 1000);
53-
// });
54-
// }
48+
function slowSquarer(num){
49+
return new Promise((resolve) => {
50+
setTimeout(() => {
51+
resolve(num * num);
52+
}, 1000);
53+
});
54+
}
5555

56-
// return chainTwoAsyncProcesses(numberPromise, slowSquarer)
57-
// .then((val) => {
56+
return chainTwoAsyncProcesses(numberPromise, slowSquarer)
57+
.then((val) => {
5858

59-
// assert.equal(val, 961);
59+
assert.equal(val, 961);
6060

61-
// const timeElapsed = new Date() - time;
61+
const timeElapsed = new Date() - time;
6262

63-
// assert(timeElapsed >= 975, 'Process too quick. Are you sure that you chained the two processes?');
63+
assert(timeElapsed >= 975, 'Process too quick. Are you sure that you chained the two processes?');
6464

65-
// assert(timeElapsed <= 1025, 'Process too slow.');
66-
// });
67-
// });
68-
// });
65+
assert(timeElapsed <= 1025, 'Process too slow.');
66+
});
67+
});
68+
});
6969
describe('#makeGetUserByIdWithOrganization(getUserById, getOrganizationById) => (id) => Promise', () => {
7070
const users = {
7171
'u001': {id: 'u001', name: 'Jeff', email: 'jeff@jeff.jeff', organizationId: 'o001'},

0 commit comments

Comments
 (0)