Skip to content

Instantly share code, notes, and snippets.

@djfm
Created September 18, 2016 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djfm/33c8b53642beec3e6556575c9f77d88d to your computer and use it in GitHub Desktop.
Save djfm/33c8b53642beec3e6556575c9f77d88d to your computer and use it in GitHub Desktop.
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