Skip to content

Instantly share code, notes, and snippets.

@jgcmarins
Forked from sibelius/filterAsync.ts
Created July 29, 2020 14:00
Show Gist options
  • Save jgcmarins/78c2d43163832af44e039cd94aaceb9e to your computer and use it in GitHub Desktop.
Save jgcmarins/78c2d43163832af44e039cd94aaceb9e to your computer and use it in GitHub Desktop.
Simple filter async
function mapAsync<T, U>(array: T[], callbackfn: (value: T, index: number, array: T[]) => Promise<U>): Promise<U[]> {
return Promise.all(array.map(callbackfn));
}
async function filterAsync<T>(array: T[], callbackfn: (value: T, index: number, array: T[]) => Promise<boolean>): Promise<T[]> {
const filterMap = await mapAsync(array, callbackfn);
return array.filter((value, index) => filterMap[index]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment