You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gristlabs_grist-core/app/common/asyncIterators.ts

26 lines
672 B

/**
* Just some basic utilities for async generators that should really be part of the language or lodash or something.
*/
export async function* asyncFilter<T>(it: AsyncIterableIterator<T>, pred: (x: T) => boolean): AsyncIterableIterator<T> {
for await (const x of it) {
if (pred(x)) {
yield x;
}
}
}
export async function* asyncMap<T, R>(it: AsyncIterableIterator<T>, mapper: (x: T) => R): AsyncIterableIterator<R> {
for await (const x of it) {
yield mapper(x);
}
}
export async function toArray<T>(it: AsyncIterableIterator<T>): Promise<T[]> {
const result = [];
for await (const x of it) {
result.push(x);
}
return result;
}