DEV Community

Discussion on: Code Smell 53 - Explicit Iteration

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

I also prefer to hide explicit iteration. Still, I had to use the first style for chunking.

const batchSize = 500

for (let i = 0; i < items.length; i += batchSize) {
  await batchInsert(items.slice(i, i + batchSize))
}
Enter fullscreen mode Exit fullscreen mode

The second style (for ... of) is for awaiting inside, or for await ... of.

The third style, I generally use map because of performance, but grammatically, it would be for each. Also, map(async () => ...) can be used with Promise.all (or Promise.allSettled).

Collapse
 
mcsee profile image
Maxi Contieri

In my opinion chinking should be hidden at iteration level.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

That is, if a convenient library function exists.