Skip to main content

Function: generateAsync()

Returns an infinite async iterable that yields seed for its first value and then yields the awaited result of applying fn to its previously yielded value for every subsequent value.

Example

import { generateAsync, pipe, reduceAsync, takeAsync, toArray } from 'lfi'

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

console.log(
await pipe(
`sleep`,
generateAsync(async previousWord => {
const response = await fetch(`${API_URL}/${previousWord}`)
const [{ meanings }] = await response.json()
return meanings[0].partOfSpeech
}),
takeAsync(4),
reduceAsync(toArray()),
),
)
//=> [ 'sleep', 'verb', 'noun', 'noun' ]
Playground

Since

v0.0.1

Call Signature

generateAsync<Value>(fn): (seed) => AsyncIterable<Value, any, any>

Type Parameters

Value

Parameters

fn

(previousValue) => MaybePromiseLike<Value>

Returns

Function

Parameters

seed

Value

Returns

AsyncIterable<Value, any, any>

Defined in

generators.d.ts:260

Call Signature

generateAsync<Value>(fn, seed): AsyncIterable<Value, any, any>

Type Parameters

Value

Parameters

fn

(previousValue) => MaybePromiseLike<Value>

seed

Value

Returns

AsyncIterable<Value, any, any>

Defined in

generators.d.ts:263