Skip to content

Instantly share code, notes, and snippets.

@cristobal
Created September 1, 2016 11:42
Show Gist options
  • Save cristobal/4758a603c7509386cd38c9f0ea6c5a1c to your computer and use it in GitHub Desktop.
Save cristobal/4758a603c7509386cd38c9f0ea6c5a1c to your computer and use it in GitHub Desktop.
Reduce a set of promises (S) over a a value x
const reducePromises = (S, x) =>
S.reduce((f, g) => f.then(g), Promise.resolve(x));
@cristobal
Copy link
Author

Example code:

const square = x => x * x
const negate = x => -x

reducePromises(3, [square, negate])
  .then(v => console.log(v)) // -9

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