Chunk Array Into Batch Iterator

Create a generator that yields fixed-size chunks from an array for memory-efficient batch processing.

JavascriptData Fetching

Code

javascriptsnippet
1 line
export function* chunkIterator(items,size){for(let i=0;i<items.length;i+=size){yield items.slice(i,i+size);}}export async function* asyncChunkIterator(asyncIterable,size){let buffer=[];for await(const item of asyncIterable){buffer.push(item);if(buffer.length>=size){yield buffer;buffer=[];}}if(buffer.length>0)yield buffer;}export function partition(items,predicate){const yes=[],no=[];for(const item of items)(predicate(item)?yes:no).push(item);return[yes,no];}

How the snippet works

Chunk Array Into Batch Iterator keeps everything in the function partition.

Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.

Where you would use it

Signature: export function* chunkIterator(items,size)

The surface worth importing is chunkIterator; the helpers around it are private to the snippet.

Interactive Tools for This Topic

FAQ

Does the code pull in any external packages?
No external packages are involved; every call resolves to a built-in API. The file defines `partition` at load time.