Skip to content

Instantly share code, notes, and snippets.

@jperasmus
Last active June 27, 2019 09:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jperasmus/2fa92093678426117e5f1837f0b30825 to your computer and use it in GitHub Desktop.
Save jperasmus/2fa92093678426117e5f1837f0b30825 to your computer and use it in GitHub Desktop.
"pipe" function that handles both sync and async functions
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