From e856ef4663c9da0f23657e1473f26a7cdeffbf0d Mon Sep 17 00:00:00 2001 From: olaszakos Date: Tue, 16 Jan 2024 18:40:40 +0100 Subject: [PATCH] add chunked-parallel from events page branch --- plugins/utils/chunked-parallel.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 plugins/utils/chunked-parallel.js diff --git a/plugins/utils/chunked-parallel.js b/plugins/utils/chunked-parallel.js new file mode 100644 index 0000000000..95b6bb32d5 --- /dev/null +++ b/plugins/utils/chunked-parallel.js @@ -0,0 +1,30 @@ +/** + * Run tasks in parallel, but limit the number of tasks running at the same time. + * @param {Array} tasks - Array of functions that return promises. + * @param {Number} chunkSize - Number of tasks to run in parallel. + * @returns {Array} - Array of results. + */ +module.exports = function chunkedParallel(tasks, chunkSize) { + return new Promise((resolve, reject) => { + const results = []; + let index = 0; + + function runNext() { + if (index >= tasks.length) { + return resolve(results); + } + + const chunk = tasks.slice(index, index + chunkSize); + index += chunkSize; + + Promise.all(chunk.map((task) => task())) + .then((chunkResults) => { + results.push(...chunkResults); + runNext(); + }) + .catch(reject); + } + + runNext(); + }); +};