Created
April 3, 2023 16:46
-
-
Save IgorHalfeld/68ee2db2c5bf9f806621f54e31ac84de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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