@vgerbot/async
    Preparing search index...

    Function groupBy

    • Groups array items by an asynchronously computed key.

      Type Parameters

      • I

        Input item type.

      Parameters

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

        Input array, or a promise that resolves to one.

      • keySelector: (item: I, token: CancellableToken) => Promise<PropertyKey>

        Async key selector for each item.

      • Optionaloptions: GroupByOptions<I>

        Configuration options, including cancellation and concurrency.

      Returns CancellableHandle<Record<string, I[]>>

      A cancellable handle resolving to grouped items.

      const handle = groupBy(
      [1, 2, 3, 4],
      async (item, token) => {
      await token.sleep(5);
      return item % 2 === 0 ? "even" : "odd";
      },
      { concurrency: 2 },
      );

      const result = await handle; // { odd: [1, 3], even: [2, 4] }
      const handle = groupBy(
      { a: 1, b: 2, c: 3 },
      async (value, key) => (key === "b" ? "special" : value % 2 ? "odd" : "even"),
      );

      const result = await handle;
      // { odd: [1, 3], special: [2] }
    • Groups array items by an asynchronously computed key.

      Type Parameters

      • I

        Input item type.

      Parameters

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

        Input array, or a promise that resolves to one.

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

        Async key selector for each item.

      • Optionaloptions: GroupByOptions<I>

        Configuration options, including cancellation and concurrency.

      Returns CancellableHandle<Record<string, I[]>>

      A cancellable handle resolving to grouped items.

      const handle = groupBy(
      [1, 2, 3, 4],
      async (item, token) => {
      await token.sleep(5);
      return item % 2 === 0 ? "even" : "odd";
      },
      { concurrency: 2 },
      );

      const result = await handle; // { odd: [1, 3], even: [2, 4] }
      const handle = groupBy(
      { a: 1, b: 2, c: 3 },
      async (value, key) => (key === "b" ? "special" : value % 2 ? "odd" : "even"),
      );

      const result = await handle;
      // { odd: [1, 3], special: [2] }