Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created July 29, 2020 12:31
Show Gist options
  • Save sibelius/e39d94c2bee2adb11f0f7239898e072d to your computer and use it in GitHub Desktop.
Save sibelius/e39d94c2bee2adb11f0f7239898e072d 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