Skip to content

Instantly share code, notes, and snippets.

@vlio20
Last active July 3, 2018 10:10
Show Gist options
  • Save vlio20/ba129972980c4d3474a551d8ee8e3d8e to your computer and use it in GitHub Desktop.
Save vlio20/ba129972980c4d3474a551d8ee8e3d8e to your computer and use it in GitHub Desktop.
batch promises (typescript)
static batchPromise(proms: (() => Promise<any>)[], batchSize: number): Promise<void> {
return this.chunck(proms, batchSize)
.reduce((step: Promise<any>, chunck) => {
return step.then(() => {
return Promise.all(chunck.map(fp => fp()));
});
}, Promise.resolve());
}
private static chunck<T>(array: T[], batchSize = 5): T[][] {
const chunked = [];
for (let i = 0; i < array.length; i += batchSize) {
chunked.push(array.slice(i, i + batchSize))
}
return chunked;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment