Last active
July 3, 2018 10:10
-
-
Save vlio20/ba129972980c4d3474a551d8ee8e3d8e to your computer and use it in GitHub Desktop.
batch promises (typescript)
This file contains 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
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