Last active
August 16, 2019 11:30
-
-
Save junaid1460/299b6a64b270094a8f370e4f3275c6ca to your computer and use it in GitHub Desktop.
typescript snippets
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
// Create batches out of big array | |
// Map those batches to a lambda | |
export async function asyncBatchMap<T, F>( | |
items: ReadonlyArray<T>, | |
predicate: (input: T[]) => Promise<F>, | |
batchSize: number = 10, | |
): Promise<F[]> { | |
const newContainer = items.map((e) => e); | |
const result: F[] = []; | |
while (newContainer.length > 0) { | |
const batch = newContainer.splice(0, batchSize); | |
const batchResult = await predicate(batch); | |
result.push(batchResult); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment