@vgerbot/async
    Preparing search index...

    Function filter

    • Filters an array of data asynchronously using a predicate function. Allows controlling the maximum concurrency of the predicate execution.

      Type Parameters

      • I

        The type of the input items.

      Parameters

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

        The array of data to filter, or a promise that resolves to one.

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

        An async function applied to each item to determine if it should be kept.

      • Optionaloptions: FilterOptions<I>

        Configuration options, including cancellation token and concurrency limit.

      Returns CancellableHandle<I[]>

      A cancellable handle that resolves to an array of the items that passed the predicate.

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

      const result = await handle; // [2, 4]
      const handle = filter(
      { a: 1, b: 2, c: 3 },
      async (value, key) => key !== "a" && value >= 2,
      );

      const result = await handle; // [2, 3]
    • Filters an array of data asynchronously using a predicate function. Allows controlling the maximum concurrency of the predicate execution.

      Type Parameters

      • I

        The type of the input items.

      Parameters

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

        The array of data to filter, or a promise that resolves to one.

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

        An async function applied to each item to determine if it should be kept.

      • Optionaloptions: FilterOptions<I>

        Configuration options, including cancellation token and concurrency limit.

      Returns CancellableHandle<I[]>

      A cancellable handle that resolves to an array of the items that passed the predicate.

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

      const result = await handle; // [2, 4]
      const handle = filter(
      { a: 1, b: 2, c: 3 },
      async (value, key) => key !== "a" && value >= 2,
      );

      const result = await handle; // [2, 3]