|
@@ -8,10 +8,12 @@ export const forEachIn = async <
|
|
>(
|
|
>(
|
|
items: ItemsType,
|
|
items: ItemsType,
|
|
callback: CallbackType,
|
|
callback: CallbackType,
|
|
- concurrency = 10
|
|
+ options: { abortOnError?: boolean; concurrency?: number } = {}
|
|
-): Promise<CallbackReturnType[]> => {
|
|
+): Promise<{ completed: CallbackReturnType[]; failed: any[] }> => {
|
|
|
|
+ const { abortOnError = true, concurrency = 10 } = options;
|
|
|
|
+
|
|
const queued = items.slice();
|
|
const queued = items.slice();
|
|
- const failed: any[] = [];
|
|
+ const failed: any[] = [];
|
|
const completed: CallbackReturnType[] = [];
|
|
const completed: CallbackReturnType[] = [];
|
|
|
|
|
|
const next = async () => {
|
|
const next = async () => {
|
|
@@ -24,6 +26,8 @@ export const forEachIn = async <
|
|
try {
|
|
try {
|
|
completed[index] = await callback(item, index);
|
|
completed[index] = await callback(item, index);
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
|
+ if (abortOnError) throw error;
|
|
|
|
+
|
|
failed[index] = error;
|
|
failed[index] = error;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -34,5 +38,5 @@ export const forEachIn = async <
|
|
Array.from(Array(Math.min(items.length, concurrency)).keys()).map(next)
|
|
Array.from(Array(Math.min(items.length, concurrency)).keys()).map(next)
|
|
);
|
|
);
|
|
|
|
|
|
- return completed;
|
|
+ return { completed, failed };
|
|
};
|
|
};
|