Skip to main content

Function: chunkAsync()

Returns an async iterable equivalent to asyncIterable except its values are grouped into arrays that each contain size values.

The last array in the returned async iterable will contain fewer than size values (but at least one) if the number of values in asyncIterable is not divisible by size.

Throws

if size is not a positive integer.

Example

console.log(
await pipe(
asAsync([1, 2, 3, 4, 5, 6, 7, 8, 9]),
chunkAsync(3),
reduceAsync(toArray()),
),
)
//=> [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]

console.log(
await pipe(
asAsync([`S`, `L`, `O`, `T`, `H`]),
chunkAsync(2),
reduceAsync(toArray()),
),
)
//=> [ [ 'S', 'L' ], [ 'O', 'T' ], [ 'H' ] ]

Since

v2.0.0

Call Signature

chunkAsync<Size>(size): <Value>(asyncIterable) => AsyncIterable<Value[], any, any>

Type Parameters

Size extends number

Parameters

size

PositiveInteger<Size>

Returns

Function

Type Parameters

Value

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncIterable<Value[], any, any>

Defined in

splices.d.ts:933

Call Signature

chunkAsync<Size, Value>(size, asyncIterable): AsyncIterable<Value[], any, any>

Type Parameters

Size extends number

Value

Parameters

size

PositiveInteger<Size>

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncIterable<Value[], any, any>

Defined in

splices.d.ts:936