Skip to content

Instantly share code, notes, and snippets.

@junaid1460
Last active August 16, 2019 11:30
Show Gist options
  • Save junaid1460/299b6a64b270094a8f370e4f3275c6ca to your computer and use it in GitHub Desktop.
Save junaid1460/299b6a64b270094a8f370e4f3275c6ca to your computer and use it in GitHub Desktop.
typescript snippets
// 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