Skip to main content

Function: cacheAsync()

cacheAsync<Value>(asyncIterable): AsyncIterable<Value, any, any>

Returns an async iterable equivalent to asyncIterable that iterates over asyncIterable at most once by lazily caching the values from the first iteration.

Type Parameters

Value

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncIterable<Value, any, any>

Example

import { asAsync, eachAsync, cacheAsync, mapAsync, pipe, reduceAsync, toArray } from 'lfi'

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

const asyncIterable = pipe(
asAsync([`sloth`, `lazy`, `sleep`]),
mapAsync(async word => {
const response = await fetch(`${API_URL}/${word}`)
return (await response.json())[0].phonetic
}),
eachAsync(console.log),
cacheAsync,
)
// No output

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

console.log(
await pipe(
asyncIterable,
reduceAsync(toArray()),
),
)
//=> [ '/slɑθ/', '/ˈleɪzi/', '/sliːp/' ]
Playground

Since

v2.0.0

Defined in

side-effects.d.ts:424