Last active
June 30, 2022 07:48
-
-
Save jperasmus/fbbcccb387896ff7db2c58797ebb76da to your computer and use it in GitHub Desktop.
"compose" function that handles both sync and async functions
This file contains hidden or 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`)) |
Awesome, I'm glad it is useful to you too. 🤗
great, very useful
could add catch if it's necessary:
const compose = (…functions) => input => functions.reduceRight((chain, func) => chain.then(func), Promise.resolve(input)).catch(err => console.warn(err));
Great!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works great. 👍