@vgerbot/async
    Preparing search index...

    Function tryEach

    • Tries executing tasks in sequence until one succeeds. Returns the result of the first successful task. If all tasks fail, throws the last error.

      Type Parameters

      • T

        The result type.

      Parameters

      Returns CancellableHandle<T>

      A cancellable handle resolving to the first successful result.

      const handle = tryEach([
      async () => { throw new Error("fail 1"); },
      async () => { throw new Error("fail 2"); },
      async () => "success",
      ]);

      const result = await handle; // "success"
      // Try multiple API endpoints
      const handle = tryEach([
      async (token) => fetch("https://api1.example.com"),
      async (token) => fetch("https://api2.example.com"),
      async (token) => fetch("https://api3.example.com"),
      ]);