How can I convert an async iterator to an array?
Here is a simple function to convert async iterator to an array without having to include a whole package.
async function toArray(asyncIterator){
const arr=[];
for await(const i of asyncIterator) arr.push(i);
return arr;
}
Your solution is the only one I know of. Here it is as a TypeScript function:
async function gen2array<T>(gen: AsyncIterable<T>): Promise<T[]> {
const out: T[] = []
for await(const x of gen) {
out.push(x)
}
return out
}
TypeScript Playground