Skip to content

Instantly share code, notes, and snippets.

@gr0uch
Last active August 29, 2015 14:16
Show Gist options
  • Save gr0uch/f9f86790c4ae93d1de11 to your computer and use it in GitHub Desktop.
Save gr0uch/f9f86790c4ae93d1de11 to your computer and use it in GitHub Desktop.
Serial execution of functions for flow based programming.
/**
* Take an array of functions that accept one argument and return a value or Promise,
* and invoke each function serially, passing the resolved value to the next function.
*
* @param {Array} fnArray
* @param {Mixed} initialValue
* @return {Promise}
*/
export default (fnArray, initialValue) =>
fnArray.reduce((chain, fn, index) =>
index === 0 ? Promise.resolve(fn(initialValue)) :
chain.then(value => fn(value)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment