Skip to content

Instantly share code, notes, and snippets.

@gregoryStarr
Created July 10, 2018 16:46
Show Gist options
  • Save gregoryStarr/ffc7e8f60a253dccabdb1cc49540c92b to your computer and use it in GitHub Desktop.
Save gregoryStarr/ffc7e8f60a253dccabdb1cc49540c92b to your computer and use it in GitHub Desktop.
chaining thens
// 77: Promise - chaining
// To do: make all tests pass, leave the assert lines unchanged!
describe('chaining multiple promises can enhance readability', () => {
describe('prerequisites for understanding', function() {
it('reminder: the test passes when a fulfilled promise is returned', function() {
return Promise.resolve('I am nice');
});
it('a function given to `then()` fulfills (if it doesnt throw)', function() {
const beNice = () => { return 'I am nice' };
return Promise.resolve()
.then(beNice)
.then(niceMessage => assert.equal(niceMessage, 'I am nice'));
});
});
describe('chain promises', function() {
const removeMultipleSpaces = string => string.replace(/\s+/g, ' ');
it('`then()` receives the result of the promise it was called on', function() {
const wordsPromise = Promise.resolve('one space between each word');
return wordsPromise
.then(string => string)
.then(actual => {assert.equal(actual, 'one space between each word')})
;
});
const appendPeriod = string => `${string}.`;
it('multiple `then()`s can be chained', function() {
const wordsPromise = Promise.resolve('Sentence without an end');
return wordsPromise
.then(removeMultipleSpaces)
.then(appendPeriod)
.then(actual => {assert.equal(actual, 'Sentence without an end.')})
;
});
const trim = string => string.replace(/^\s+/, '').replace(/\s+$/, '');
it('order of the `then()`s matters', function() {
const wordsPromise = Promise.resolve('Sentence without an end ');
return wordsPromise
.then(trim)
.then(removeMultipleSpaces)
.then(appendPeriod)
.then(actual => {assert.equal(actual, 'Sentence without an end.')})
;
});
const asyncUpperCaseStart = (string, onDone) => {
const format = () => onDone(string[0].toUpperCase() + string.substr(1));
setTimeout(format, 100);
};
it('any of the things given to `then()` can resolve asynchronously (the real power of Promises)', function() {
const wordsPromise = Promise.resolve('sentence without an end');
return wordsPromise
.then(string => new Promise(resolve => asyncUpperCaseStart(string, resolve)))
.then(string => new Promise(resolve => setTimeout(() => resolve(appendPeriod(string)), 100)))
.then(actual => {assert.equal(actual, 'Sentence without an end.')})
;
});
it('also asynchronously, the order still matters, promises wait, but don`t block', function() {
const wordsPromise = Promise.resolve('trailing space ');
return wordsPromise
.then(string => new Promise(resolve => asyncUpperCaseStart(string, resolve)))
.then(string => new Promise(resolve => setTimeout(() => resolve(trim(string)), 100)))
.then(string => new Promise(resolve => setTimeout(() => resolve(appendPeriod(string)), 100)))
.then(actual => {assert.equal(actual, 'Trailing space.')})
;
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment