Skip to main content

Variable: chunkAsync()

const chunkAsync: {<Size>(size): <Value>(asyncIterable) => AsyncIterable<Value[]>; <Size, Value>(size, asyncIterable): AsyncIterable<Value[]>; }

Defined in: splices.d.ts:1065

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.

Call Signature​

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

Type Parameters​

Size​

Size extends number

Parameters​

size​

PositiveInteger<Size>

Returns​

<Value>(asyncIterable): AsyncIterable<Value[]>

Type Parameters​

Value​

Value

Parameters​

asyncIterable​

AsyncIterable<Value>

Returns​

AsyncIterable<Value[]>

Call Signature​

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

Type Parameters​

Size​

Size extends number

Value​

Value

Parameters​

size​

PositiveInteger<Size>

asyncIterable​

AsyncIterable<Value>

Returns​

AsyncIterable<Value[]>

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