@vgerbot/async
    Preparing search index...

    Function series

    Executes multiple tasks in series, chaining their outputs as inputs to subsequent tasks. Each task receives the output of the previous task as input and a cancellation token. If any task is cancelled or throws an error, the entire series is aborted.

    Tasks can be:

    • Functions that receive input and a cancellation token
    • Promises that resolve to a value
    • CancellableHandle instances

    The series execution respects cancellation through the provided options and will:

    • Check for cancellation after each task completes
    • Propagate cancellation to CancellableHandle tasks
    • Throw CancelError if cancelled during execution

    Cancellation options including signal, retry configuration, and timeout

    The first task to execute (receives void as input)

    Additional tasks to execute in sequence

    A CancellableHandle that resolves to the last task's output

    If the series is cancelled during execution

    const controller = new AbortController();
    const handle = series(
    { signal: controller.signal },
    async (_, token) => fetch('/api/user', { signal: token.signal }),
    async (response, token) => {
    await token.sleep(100);
    return response.json();
    },
    async (data) => processData(data)
    );

    // Cancel the series
    controller.abort();
    • Executes a single task in series with cancellation support.

      Type Parameters

      • T1

        The output type of the task

      Parameters

      • options: CancellableOptions

        Cancellation options for the series execution

      • task1: SeriesTask<void, T1>

        The task to execute

      Returns CancellableHandle<T1>

      A CancellableHandle that resolves to the task's output

      const handle = series(
      { signal: abortController.signal },
      async (input, token) => {
      await token.sleep(1000);
      return "result";
      }
      );
    • Executes two tasks in series, passing the output of the first task as input to the second.

      Type Parameters

      • T1

        The output type of the first task

      • T2

        The output type of the second task

      Parameters

      • options: CancellableOptions

        Cancellation options for the series execution

      • task1: SeriesTask<void, T1>

        The first task to execute

      • task2: SeriesTask<T1, T2>

        The second task to execute, receives T1 as input

      Returns CancellableHandle<T2>

      A CancellableHandle that resolves to the second task's output

      const handle = series(
      { signal: abortController.signal },
      async () => 42,
      async (num, token) => `Result: ${num}`
      );
    • Executes three tasks in series, chaining their outputs as inputs to subsequent tasks.

      Type Parameters

      • T1

        The output type of the first task

      • T2

        The output type of the second task

      • T3

        The output type of the third task

      Parameters

      • options: CancellableOptions

        Cancellation options for the series execution

      • task1: SeriesTask<void, T1>

        The first task to execute

      • task2: SeriesTask<T1, T2>

        The second task to execute, receives T1 as input

      • task3: SeriesTask<T2, T3>

        The third task to execute, receives T2 as input

      Returns CancellableHandle<T3>

      A CancellableHandle that resolves to the third task's output

      const handle = series(
      { signal: abortController.signal },
      async () => 1,
      async (n) => n + 1,
      async (n) => n * 2
      );
      // Result: 4
    • Executes four tasks in series, chaining their outputs as inputs to subsequent tasks.

      Type Parameters

      • T1

        The output type of the first task

      • T2

        The output type of the second task

      • T3

        The output type of the third task

      • T4

        The output type of the fourth task

      Parameters

      • options: CancellableOptions

        Cancellation options for the series execution

      • task1: SeriesTask<void, T1>

        The first task to execute

      • task2: SeriesTask<T1, T2>

        The second task to execute, receives T1 as input

      • task3: SeriesTask<T2, T3>

        The third task to execute, receives T2 as input

      • task4: SeriesTask<T3, T4>

        The fourth task to execute, receives T3 as input

      Returns CancellableHandle<T4>

      A CancellableHandle that resolves to the fourth task's output

      const handle = series(
      { signal: abortController.signal },
      async () => "hello",
      async (str) => str.toUpperCase(),
      async (str) => str + " WORLD",
      async (str) => str.length
      );
      // Result: 11