Last active
October 11, 2020 22:28
-
-
Save jozefcipa/2518a39ab5702bdabfbbf2fd49f39c28 to your computer and use it in GitHub Desktop.
Process lots of promises effectively
This file contains hidden or 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
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