Skip to content

Instantly share code, notes, and snippets.

@Kelin2025
Created October 2, 2017 21:28
Show Gist options
  • Save Kelin2025/4515a9d17e27e73e082444d9a159b63f to your computer and use it in GitHub Desktop.
Save Kelin2025/4515a9d17e27e73e082444d9a159b63f to your computer and use it in GitHub Desktop.
const asyncPipe = (...fns) => arg => {
const res = fns[0](arg)
const slice = fns.slice(1)
const next = slice.length ? asyncPipe(...slice) : identity
const isPromise = typeof res === 'object' && 'then' in res
return isPromise ? res.then(next) : next(res)
}
const add = a => b => a + b
asyncPipe(
Promise.resolve,
add(1),
Promise.resolve,
Promise.resolve,
add(1)
)(1) // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment