-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
28 lines (25 loc) · 861 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
export async function fetchAllItems<T>(
func: (
params: { size: number; page: number },
) => Promise<{ data: T[]; page: number | null; pages?: number | null }>,
items: T[] = [],
page = 1,
): Promise<T[]> {
const response = await func({ size: 100, page });
const data = response.data as T[];
items = [...items, ...data];
if ((response.page ?? page) < (response.pages ?? 0)) {
return fetchAllItems(func, items, page + 1);
}
return items;
}
export async function asyncFilter<T>(
arr: T[],
predicate: (value: T, index: number, array: T[]) => Promise<boolean>,
): Promise<T[]> {
return (await Promise.all(arr.map(async (item, i, arr2) => {
const success = await predicate(item, i, arr2);
return { item, success };
}))).filter((t) => t.success)
.map((t) => t.item);
}