waterfall

async

Runs async functions in series, passing the result of each function to the next. Similar to Array.reduce but for async operations.

Signature

waterfall<T>(tasks: ((value: any) => Promise<any>)[], initialValue?: T): Promise<any>

Parameters

NameTypeDescription
tasks((value: any) => Promise<any>)[]Array of async functions where each receives the previous result
initialValue?TInitial value to pass to the first function

Returns

Promise<any> - Returns the final result

Examples

Chain operations

import { waterfall } from 'dashlite'

const result = await waterfall([
  async () => 1,
  async (n) => n + 1,
  async (n) => n * 2
])
console.log(result)

Output:

4

Data pipeline

import { waterfall } from 'dashlite'

await waterfall([
  async (data) => processStep1(data),
  async (result1) => processStep2(result1),
  async (result2) => processStep3(result2)
], initialData)

Available since version 1.2.0