Last active
June 27, 2019 09:52
-
-
Save jperasmus/2fa92093678426117e5f1837f0b30825 to your computer and use it in GitHub Desktop.
"pipe" function that handles both sync and async functions
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 pipe = (…functions) => input => functions.reduce((chain, func) => chain.then(func), Promise.resolve(input)); | |
// Functions fn1, fn2, fn3 can be standard synchronous functions or return a Promise | |
pipe(fn1, fn2, fn3)(input).then(result => console.log(`Do with the ${result} as you please`)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment