Skip to content

Instantly share code, notes, and snippets.

@fersilva16
Created February 4, 2022 17:58
Show Gist options
  • Save fersilva16/dea9bb85ca69de435d01b8793d44948a to your computer and use it in GitHub Desktop.
Save fersilva16/dea9bb85ca69de435d01b8793d44948a to your computer and use it in GitHub Desktop.
Async array filter
import { asyncEach } from './asyncEach'; // From https://gist.github.com/fersilva16/bdf2d0b9a5eedbcf7232760df876a87d
export async function asyncFilter<T>(
array: { map(callback: (value: T) => any): Promise<any>[] },
predicate: (value: T) => Promise<boolean>
): Promise<T[]> {
const result: T[] = [];
await asyncEach(array, async (value, ...args) => {
const shouldAdd = await predicate(value, ...args);
if (shouldAdd) result.push(value);
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment