UNPKG

actionhero

Version:

actionhero.js is a multi-transport API Server with integrated cluster capabilities and delayed tasks

59 lines (58 loc) 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.asyncWaterfall = void 0; /** * In series, run an array of `async` functions * * without arguments * ```js * let sleepyFunc = async () => { * await new Promise((resolve) => { setTimeout(resolve, 100) }) * return (new Date()).getTime() * } * let jobs = [sleepyFunc, sleepyFunc, sleepyFunc] * let responses = await api.utils.asyncWaterfall(jobs) * // responses = [1506536188356, 1506536188456, 1506536188456] * ``` * * with arguments * ```js * let sleepyFunc = async (response) => { * await new Promise((resolve) => { setTimeout(resolve, 100) }) * return response * } * let jobs = [ * {method: sleepyFunc, args: ['a']}, * {method: sleepyFunc, args: ['b']}, * {method: sleepyFunc, args: ['c']} * ] * let responses = await api.utils.asyncWaterfall(jobs) * // responses = ['a', 'b', 'c'] * ``` */ async function asyncWaterfall(jobs) { const results = []; while (jobs.length > 0) { const collection = jobs.shift(); let job; let args; if (typeof collection === "function") { job = collection; args = []; } else { job = collection.method; args = collection.args; } const value = await job.apply(this, args); results.push(value); } if (results.length === 0) { return null; } if (results.length === 1) { return results[0]; } return results; } exports.asyncWaterfall = asyncWaterfall;