Skip to main content

Variable: cacheAsync()

const cacheAsync: <Value>(asyncIterable) => AsyncIterable<Value>

Defined in: side-effects.d.ts:424

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

Value

Parameters

asyncIterable

AsyncIterable<Value>

Returns

AsyncIterable<Value>

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