/** * @license * Copyright 2023 Google Inc. * SPDX-License-Identifier: Apache-2.0 */ import type {AwaitableIterable} from '../common/types.js'; /** * @internal */ export class AsyncIterableUtil { static async *map( iterable: AwaitableIterable, map: (item: T) => Promise, ): AsyncIterable { for await (const value of iterable) { yield await map(value); } } static async *flatMap( iterable: AwaitableIterable, map: (item: T) => AwaitableIterable, ): AsyncIterable { for await (const value of iterable) { yield* map(value); } } static async collect(iterable: AwaitableIterable): Promise { const result = []; for await (const value of iterable) { result.push(value); } return result; } static async first( iterable: AwaitableIterable, ): Promise { for await (const value of iterable) { return value; } return; } }