funcRunner.js 604 B

12345678910111213141516171819202122
  1. //
  2. 'use strict';
  3. const chainFuncsAsync = (result, func) => result.then(func);
  4. const chainFuncsSync = (result, func) => func(result);
  5. /**
  6. * Runs the given functions sequentially. If the `init` param is a promise,
  7. * functions are chained using `p.then()`. Otherwise, functions are chained by passing
  8. * the result of each function to the next.
  9. */
  10. module.exports = function funcRunner(
  11. init ,
  12. funcs
  13. ) {
  14. const isAsync = init instanceof Promise;
  15. return funcs.reduce(
  16. isAsync === true ? chainFuncsAsync : chainFuncsSync,
  17. init
  18. );
  19. };