2022-03-22 16:29:53 -04:00
|
|
|
/**
|
|
|
|
* Maps each value of an array into an async function, and returns an array of the results
|
|
|
|
* @param {array} arr - array of values
|
|
|
|
* @param {number} batchSize - number of calls to `func` to do at one time
|
|
|
|
* @param {function} func - async function to apply to all items in `arr`. Function should take one argument.
|
|
|
|
* @return {array} - results of `func` applied to each item in `arr`
|
|
|
|
*/
|
|
|
|
exports.batchMap = async (arr, batchSize, func) => {
|
|
|
|
const results = [];
|
|
|
|
for (let i = 0; i < arr.length; i += batchSize) {
|
2022-09-26 15:16:18 -04:00
|
|
|
const result = await Promise.all(
|
2022-03-22 16:29:53 -04:00
|
|
|
arr.slice(i, i + batchSize).map(func)
|
|
|
|
);
|
|
|
|
results.push(...result);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
};
|