Skip to content

Instantly share code, notes, and snippets.

@munkhorgil
Created October 3, 2019 08:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save munkhorgil/0a3993c3e29403a42c2ff92c97fb8d20 to your computer and use it in GitHub Desktop.
Save munkhorgil/0a3993c3e29403a42c2ff92c97fb8d20 to your computer and use it in GitHub Desktop.
Compose async functions right to left
/**
* 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