Skip to content

Instantly share code, notes, and snippets.

@phpsmarter
Forked from jperasmus/compose.js
Created March 29, 2018 01:18
Show Gist options
  • Save phpsmarter/88d7dc0c96ffd6fe785f5b75cfbc4cf0 to your computer and use it in GitHub Desktop.
Save phpsmarter/88d7dc0c96ffd6fe785f5b75cfbc4cf0 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