Skip to main content

Function: sliceAsync()

Returns an async iterable containing the values of asyncIterable between start and end (exclusive) of asyncIterable.

If any part of the range between start and end is outside the bounds of the async iterable, then that part is excluded from the returned async iterable. Thus, the returned async iterable may be empty.

WARNING: This function linearly iterates up to end because async iterables do not support random access.

Throws

if either start or end is not a non-negative integer, or if start is greater than end.

Example

const asyncIterable = asAsync([`sloth`, `more sloth`, `even more sloth`])

console.log(
await pipe(
asyncIterable,
sliceAsync(0, 3),
reduceAsync(toArray()),
),
)
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(
await pipe(
asyncIterable,
sliceAsync(0, 42),
reduceAsync(toArray()),
),
)
//=> [ 'sloth', 'more sloth', 'even more sloth' ]

console.log(
await pipe(
asyncIterable,
sliceAsync(1, 3),
reduceAsync(toArray()),
),
)
//=> [ 'more sloth', 'even more sloth' ]

console.log(
await pipe(
asyncIterable,
sliceAsync(3, 5),
reduceAsync(toArray()),
),
)
//=> []

Since

v3.5.0

Call Signature

sliceAsync<Start>(start): <End>(End) => <Value>(asyncIterable) => AsyncIterable<Value, any, any><End, Value>(End, asyncIterable) => AsyncIterable<Value, any, any>

Type Parameters

Start extends number

Parameters

start

NonNegativeInteger<Start>

Returns

Function

Type Parameters

End extends number

Parameters

End

NonNegativeInteger<End>

Returns

Function

Type Parameters

Value

Parameters
asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncIterable<Value, any, any>

Type Parameters

End extends number

Value

Parameters

End

NonNegativeInteger<End>

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncIterable<Value, any, any>

Defined in

splices.d.ts:638

Call Signature

sliceAsync<Start, End>(start, End): <Value>(asyncIterable) => AsyncIterable<Value, any, any>

Type Parameters

Start extends number

End extends number

Parameters

start

NonNegativeInteger<Start>

End

NonNegativeInteger<End>

Returns

Function

Type Parameters

Value

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncIterable<Value, any, any>

Defined in

splices.d.ts:650

Call Signature

sliceAsync<Start, End, Value>(start, End, asyncIterable): AsyncIterable<Value, any, any>

Type Parameters

Start extends number

End extends number

Value

Parameters

start

NonNegativeInteger<Start>

End

NonNegativeInteger<End>

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncIterable<Value, any, any>

Defined in

splices.d.ts:655