Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/async-array-utils. A collection of array-related async utilities.
array array-methods async promise utils
Something went wrong. Try again.
817 B · 33 lines
TypeScript
at vitest-5
12345678910111213141516171819202122232425262728293031323334import asyncForEach from './forEach.js';
function asyncFilter<T>( arr: T[], fn: (cur: T, idx: number, arr: T[]) => Promise<false>,): Promise<never[]>;function asyncFilter<T>( arr: T[], fn: (cur: T, idx: number, arr: T[]) => Promise<boolean>,): Promise<T[]>;function asyncFilter<T>( arr: T[], fn: (cur: T, idx: number, arr: T[]) => Promise<boolean>,): Promise<T[] | never[]> { const result: T[] = [];
return asyncForEach<T, void>( arr, (cur, idx, arr2) => new Promise((resolve, reject) => { fn(cur, idx, arr2) .then((cond) => { if (cond) { result[idx] = cur; } resolve(); }) .catch(reject); }), ).then(() => result.filter((_cur, idx) => idx in result));}
export default asyncFilter;