-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpromises.ts
More file actions
30 lines (29 loc) · 955 Bytes
/
promises.ts
File metadata and controls
30 lines (29 loc) · 955 Bytes
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
29
30
export function groupByStatus<T>(results: PromiseSettledResult<T>[]): {
fulfilled: PromiseFulfilledResult<T>[];
rejected: PromiseRejectedResult[];
} {
return results.reduce<{
fulfilled: PromiseFulfilledResult<T>[];
rejected: PromiseRejectedResult[];
}>(
(acc, result) =>
result.status === 'fulfilled'
? { ...acc, fulfilled: [...acc.fulfilled, result] }
: { ...acc, rejected: [...acc.rejected, result] },
{ fulfilled: [], rejected: [] },
);
}
export async function asyncSequential<TInput, TOutput>(
items: TInput[],
work: (item: TInput) => Promise<TOutput>,
): Promise<TOutput[]> {
// for-loop used instead of reduce for performance
const results: TOutput[] = [];
// eslint-disable-next-line functional/no-loop-statements
for (const item of items) {
const result = await work(item);
// eslint-disable-next-line functional/immutable-data
results.push(result);
}
return results;
}