Skip to content

Instantly share code, notes, and snippets.

@IgorHalfeld
Created April 3, 2023 16:46
Show Gist options
  • Save IgorHalfeld/68ee2db2c5bf9f806621f54e31ac84de to your computer and use it in GitHub Desktop.
Save IgorHalfeld/68ee2db2c5bf9f806621f54e31ac84de to your computer and use it in GitHub Desktop.
export async function processPromisesBatch(
items: Array<any>,
limit: number,
fn: (item: any) => Promise<any>,
): Promise<any> {
let results = [];
for (let start = 0; start < items.length; start += limit) {
const end = start + limit > items.length ? items.length : start + limit;
const slicedResults = await Promise.all(items.slice(start, end).map(fn));
results = [
...results,
...slicedResults,
]
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment