Skip to main content

Function: nextAsync()

nextAsync<Value>(asyncIterable): Promise<[AsyncOptional<Value>, AsyncIterable<Value, any, any>]>

Returns a promise that resolves to a pair of async iterables. If asyncIterable is empty, then both of the returned async iterables are empty. Otherwise, the first async iterable contains the first value of asyncIterable and the second async iterable contains the rest of the values of asyncIterable. The second async iterable can only be iterated once.

Type Parameters

Value

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

Promise<[AsyncOptional<Value>, AsyncIterable<Value, any, any>]>

Example

import { asAsync, countAsync, emptyAsync, getAsync, mapAsync, nextAsync, pipe, reduceAsync, toArray } from 'lfi'

const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`

const [first, rest] = await pipe(
asAsync([`sloth`, `lazy`, `sleep`]),
mapAsync(async word => {
const response = await fetch(`${API_URL}/${word}`)
return (await response.json())[0].phonetic
}),
nextAsync,
)

console.log(await getAsync(first))
//=> /slɑθ/

console.log(
await pipe(
rest,
reduceAsync(toArray()),
),
)
//=> [ '/ˈleɪzi/', '/sliːp/' ]

const [first2, rest2] = await nextAsync(emptyAsync)

console.log(await countAsync(first2))
//=> 0

console.log(await countAsync(rest2))
//=> 0
Playground

Since

v0.0.1

Defined in

optionals.d.ts:357