asyncerator
Version:
Provide supporting types for AsyncIterable/AsyncIterableIterators, promisified stream.pipeline implementation, and Array-like utility operators, sources and sinks.
23 lines (19 loc) • 631 B
text/typescript
// sink/to-array.ts
/*
* Copyright (c) 2021-2024 Check Digit, LLC
*
* This code is licensed under the MIT license (see LICENSE.txt for details).
*/
import asyncerator, { type Asyncable } from '../asyncerator';
/**
* Turn an async iterable iterator into an Array.
* This will wait until the iterator is done before returning an array, so be careful using this
* with endless iterators (in other words, don't do that).
*/
export default async function <T>(iterator: Asyncable<T>): Promise<T[]> {
const results = [];
for await (const result of asyncerator(iterator)) {
results.push(result);
}
return results;
}