Created
October 2, 2017 21:28
-
-
Save Kelin2025/4515a9d17e27e73e082444d9a159b63f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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