-
-
Save EvgenyArtemov/2957c642c2a2fda9358b6d15d055a8bc to your computer and use it in GitHub Desktop.
"compose" 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
// Async compose | |
const compose = (…functions) => input => functions.reduceRight((chain, func) => chain.then(func), Promise.resolve(input)); | |
// Functions fn1, fn2, fn3 can be standard synchronous functions or return a Promise | |
compose(fn3, fn2, fn1)(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