UNPKG

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