computer-scienceangular-roadmapbackend-roadmapblockchain-roadmapdba-roadmapdeveloper-roadmapdevops-roadmapfrontend-roadmapgo-roadmaphactoberfestjava-roadmapjavascript-roadmapnodejs-roadmappython-roadmapqa-roadmapreact-roadmaproadmapstudy-planvue-roadmapweb3-roadmap
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
48 lines
1.3 KiB
10 months ago
|
type PromiseInput<T> = Array<Promise<T> | (() => Promise<T>)>;
|
||
|
|
||
|
/**
|
||
|
* Run promises in parallel with a variable batch size.
|
||
|
*
|
||
|
* @category Promise
|
||
|
* @param promises Array of promises to run in parallel
|
||
|
* @param batchSize Number of promises to run concurrently
|
||
|
* @param ignoreErrors Whether to ignore errors and continue running promises even if some fail
|
||
|
* @returns Promise that resolves when all promises are settled
|
||
|
*/
|
||
|
export async function runPromisesInBatchSequentially<T>(
|
||
|
promises: PromiseInput<T>,
|
||
|
batchSize: number,
|
||
|
ignoreErrors = false,
|
||
|
): Promise<T[]> {
|
||
|
const results: T[] = [];
|
||
|
|
||
|
for (let i = 0; i < promises.length; i += batchSize) {
|
||
|
const batch = promises.slice(i, i + batchSize);
|
||
|
await Promise.all(
|
||
|
batch.map((promise) => {
|
||
|
if (promise instanceof Promise) {
|
||
|
return promise
|
||
|
.then((result) => results.push(result))
|
||
|
.catch((error) => {
|
||
|
if (!ignoreErrors) {
|
||
|
throw error;
|
||
|
}
|
||
|
return null;
|
||
|
});
|
||
|
} else {
|
||
|
return promise()
|
||
|
.then((result) => results.push(result))
|
||
|
.catch((error) => {
|
||
|
if (!ignoreErrors) {
|
||
|
throw error;
|
||
|
}
|
||
|
return null;
|
||
|
});
|
||
|
}
|
||
|
}),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return results;
|
||
|
}
|