Created
September 18, 2016 12:54
-
-
Save djfm/33c8b53642beec3e6556575c9f77d88d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chain = (...fns) => | |
initialValue => fns.reduce( | |
(result, fn) => { | |
if (result && (typeof result === 'object')) { | |
const then = result.then; | |
if (typeof then === 'function') { | |
return then.call(result, fn); | |
} | |
} | |
return fn(result); | |
}, | |
initialValue | |
); | |
chain( | |
x => Promise.resolve(x + 1), | |
x => x / 2, | |
x => Promise.resolve(x / 2) | |
)(0) | |
.should.eventually.equal(0.25); | |
// run it: https://jsfiddle.net/jgj4eh68/1/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment