Tweakr
    Preparing search index...

    Function waterfall

    • Executes asynchronous tasks sequentially, passing the result of each task as input to the next one (waterfall pattern).

      Type Parameters

      • T

        Type of the initial input value.

      Parameters

      • tasks: ((input: any) => Promise<any>)[]

        An array of async functions, each receiving the previous result as input.

      • initial: T

        The initial input value for the first task.

      Returns Promise<any>

      A promise resolving to the final task’s result.

      const tasks = [
      async (x: number) => x + 1,
      async (x: number) => x * 2,
      async (x: number) => x - 3
      ];

      const result = await waterfall(tasks, 5);
      console.log(result); // ((5 + 1) * 2) - 3 = 9

      1.1.0