Skip to content

Instantly share code, notes, and snippets.

@jozefcipa
Last active October 11, 2020 22:28
Show Gist options
  • Save jozefcipa/2518a39ab5702bdabfbbf2fd49f39c28 to your computer and use it in GitHub Desktop.
Save jozefcipa/2518a39ab5702bdabfbbf2fd49f39c28 to your computer and use it in GitHub Desktop.
Process lots of promises effectively
import chunk from 'lodash.chunk'
export const processInChunks = async (array, handlerFn, { chunkSize = 5 } = {}) => {
const result = []
for (const dataChunk of chunk(array, chunkSize)) {
result.push(...await Promise.all(dataChunk.map(handlerFn)))
}
return result
}
// Example of using
//
// const files = [ ... ] // lot of files that would generate lot of promises that you don't want to run at once
//
// await processInChunks(files, async fileKey => {
// await storageService.deleteFile(key)
//})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment