Files
DiceCloud/app/imports/api/utility/asyncMap.ts
2024-02-20 23:21:12 +02:00

20 lines
519 B
TypeScript

/**
* Async compatible map that processes all items in parallel
*/
export async function parallelMap(array: any[], fn: (doc: any) => Promise<any>): Promise<any[]> {
return await Promise.all(array.map(fn));
}
/**
* Async compatible map that processes all items in series
*/
export async function serialMap(array: any[], fn: (doc: any) => Promise<any>): Promise<any[]> {
const results: any[] = [];
for (const doc of array) {
const result = await fn(doc);
results.push(result);
}
return results;
}