refactor: minimize review fixes
This commit is contained in:
1 parent
396b78aa3e
commit
7a56a7d74f
3 files changed
+18
-35
No files matched your search
@@ -1,23 +0,0 @@
|
||||
export async function mapWithConcurrency<T, U>(
|
||||
values: readonly T[],
|
||||
concurrency: number,
|
||||
mapper: (value: T, index: number) => Promise<U>,
|
||||
): Promise<U[]> {
|
||||
if (!Number.isSafeInteger(concurrency) || concurrency < 1) {
|
||||
throw new Error("Concurrency must be a positive integer");
|
||||
}
|
||||
|
||||
const results: U[] = [];
|
||||
let nextIndex = 0;
|
||||
const workerCount = Math.min(concurrency, values.length);
|
||||
const workers = Array.from({ length: workerCount }, async () => {
|
||||
while (nextIndex < values.length) {
|
||||
const index = nextIndex;
|
||||
nextIndex += 1;
|
||||
results[index] = await mapper(values[index] as T, index);
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.all(workers);
|
||||
return results;
|
||||
}
|
||||
@@ -13,4 +13,3 @@ export * from "./theme";
|
||||
export * from "./function";
|
||||
export * from "./grid-algorithm";
|
||||
export * from "./id";
|
||||
export * from "./async";
|
||||
Reference in new issue
Block a user