Created
October 3, 2019 08:28
-
-
Save munkhorgil/0a3993c3e29403a42c2ff92c97fb8d20 to your computer and use it in GitHub Desktop.
Compose async functions right to left
This file contains 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
/** | |
* Compose async functions | |
* @param {Functions} fns | |
* @returns {Promise} fns value | |
*/ | |
const compose = (...fns) => arg => fns.reduceRight((p, f) => p.then(f), Promise.resolve(arg)); | |
const foo = a => a + 1; | |
const bar = b => b + 1; | |
const result = compose(foo, bar)(1); | |
console.log(result) // 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment