@vgerbot/async
    Preparing search index...

    Function detect

    • Finds the first item whose async predicate resolves to true. With concurrency > 1, it short-circuits scheduling new work after a match is found.

      Type Parameters

      • I

        The input item type.

      Parameters

      • data: I[] | Promise<I[]>

        Input array, or a promise that resolves to one.

      • predicate: (item: I, token: CancellableToken) => Promise<boolean>

        Async predicate to evaluate.

      • Optionaloptions: DetectOptions<I>

        Configuration options including cancellation and concurrency.

      Returns CancellableHandle<I | undefined>

      A cancellable handle resolving to the matched item or undefined.

      const handle = detect(
      [1, 2, 3, 4],
      async (item, token) => {
      await token.sleep(10);
      return item > 2;
      },
      { concurrency: 2 },
      );

      const found = await handle; // 3
      const handle = detect(
      { a: 1, b: 4, c: 2 },
      async (value, key) => key === "b" && value > 3,
      );

      const found = await handle; // 4
    • Finds the first item whose async predicate resolves to true. With concurrency > 1, it short-circuits scheduling new work after a match is found.

      Type Parameters

      • I

        The input item type.

      Parameters

      • data: CollectionInput<I> | Promise<CollectionInput<I>>

        Input array, or a promise that resolves to one.

      • predicate: (item: I, key: string | number, token: CancellableToken) => Promise<boolean>

        Async predicate to evaluate.

      • Optionaloptions: DetectOptions<I>

        Configuration options including cancellation and concurrency.

      Returns CancellableHandle<I | undefined>

      A cancellable handle resolving to the matched item or undefined.

      const handle = detect(
      [1, 2, 3, 4],
      async (item, token) => {
      await token.sleep(10);
      return item > 2;
      },
      { concurrency: 2 },
      );

      const found = await handle; // 3
      const handle = detect(
      { a: 1, b: 4, c: 2 },
      async (value, key) => key === "b" && value > 3,
      );

      const found = await handle; // 4