Skip to content

Instantly share code, notes, and snippets.

@GheorgheP
Last active July 2, 2017 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GheorgheP/9351f16b51832cbc6d2e7565d20e8bcb to your computer and use it in GitHub Desktop.
Save GheorgheP/9351f16b51832cbc6d2e7565d20e8bcb to your computer and use it in GitHub Desktop.
composeAsync
const composeAsync = (...fs) => {
if (fs.length === 0) {
throw new Error('compose requires at least one argument')
}
return async (...as) => {
const call = async fs => {
const f = fs[0]
const tail = fs.slice(1, Infinity)
return await tail.length === 0 ? f(...as) : f(await call(tail))
}
return await call(fs)
}
}
@GheorgheP
Copy link
Author

GheorgheP commented Jul 1, 2017

Function composition with for asynchronous functions.

Test: https://repl.it/HT3C/4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment