Skip to content

Instantly share code, notes, and snippets.

@EvgenyArtemov
Forked from jperasmus/compose.js
Created June 30, 2022 07:48
Show Gist options
  • Save EvgenyArtemov/2957c642c2a2fda9358b6d15d055a8bc to your computer and use it in GitHub Desktop.
Save EvgenyArtemov/2957c642c2a2fda9358b6d15d055a8bc to your computer and use it in GitHub Desktop.
"compose" function that handles both sync and async functions
// 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