Skip to content

Instantly share code, notes, and snippets.

@tomardern
Created June 3, 2022 20:28
Show Gist options
  • Save tomardern/5f32d3d5acba26a4abd2c96e1bdce259 to your computer and use it in GitHub Desktop.
Save tomardern/5f32d3d5acba26a4abd2c96e1bdce259 to your computer and use it in GitHub Desktop.
Chunk Promise Array with Timeout
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