rubico
Version:
[a]synchronous functional programming
28 lines (25 loc) • 721 B
JavaScript
const isPromise = require('./isPromise')
const promiseAll = require('./promiseAll')
/**
* @name functionArrayAll
*
* @synopsis
* ```coffeescript [specscript]
* functionArrayAll(funcs Array<function>, args Array) -> Promise|Array
* ```
*/
const functionArrayAll = function (funcs, args) {
const funcsLength = funcs.length,
result = Array(funcsLength)
let funcsIndex = -1, isAsync = false
while (++funcsIndex < funcsLength) {
const f = funcs[funcsIndex]
const resultItem = typeof f == 'function' ? f(...args) : f
if (isPromise(resultItem)) {
isAsync = true
}
result[funcsIndex] = resultItem
}
return isAsync ? promiseAll(result) : result
}
module.exports = functionArrayAll