Skip to content

Commit b76ecf2

Browse files
author
JoelCodes
committed
Wrote flat map function and test
1 parent fe1cadc commit b76ecf2

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

answers/chaining-promises.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,24 @@
1+
/**
2+
*
3+
* EXERCISE 1
4+
*
5+
* Just as we did with mapPromise, we want to envision a new way of transforming promises: chaining them. We want to be able to have one promise resolve, then when it's done, use its value to start a second async process that returns a promise.
6+
*
7+
* WE DON'T WANT TO START THAT SECOND PROCESS UNTIL AFTER THAT FIRST PROCESS RESOLVES! In this way, we can imagine that we may need some artifact from the first process before we can start the second. For instance, we may want to create a User object in a database, get an id back, and then use that UserId to create a series of, say, services that this user can access.
8+
*
9+
* This chaining is a common problem in async, so let's create a function to do this.
10+
*
11+
* @param {Promise} promise
12+
* @param {*} asyncTransformer
13+
*/
14+
function flatMapPromise(promise, asyncTransformer){
15+
return new Promise((resolve, reject) => {
16+
promise
17+
.then(/* IMPLEMENT ME! */);
18+
});
19+
}
120

2-
module.exports = {};
21+
22+
module.exports = {
23+
flatMapPromise,
24+
};

test/chaining-promises.test.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
11
/* eslint-env mocha */
22

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

0 commit comments

Comments
 (0)