Created
June 3, 2022 20:28
-
-
Save tomardern/5f32d3d5acba26a4abd2c96e1bdce259 to your computer and use it in GitHub Desktop.
Chunk Promise Array with Timeout
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
function chunkPromises(array: Promise<any>[], size: number, delay: number): Promise<any> { | |
let p: Promise<any> = Promise.resolve(); | |
let results = []; | |
for (var i = 0; i < array.length; i += size) { | |
p = p.then(() => i > 0 ? new Promise((r) => setTimeout(r, delay)) : true) | |
.then(() => Promise.all(array.slice(i, i + size))) | |
.then((r) => results.push(...r)); | |
} | |
return p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment