Created
February 4, 2022 17:58
-
-
Save fersilva16/dea9bb85ca69de435d01b8793d44948a to your computer and use it in GitHub Desktop.
Async array filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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