Skip to main content

Function: cacheConcur()

cacheConcur<Value>(concurIterable): ConcurIterable<Value>

Returns a concur iterable equivalent to concurIterable that iterates over concurIterable at most once by lazily caching the values from the first iteration.

Type Parameters

Value

Parameters

concurIterable

ConcurIterable<Value>

Returns

ConcurIterable<Value>

Example

import { asConcur, eachConcur, cacheConcur, mapConcur, pipe, reduceConcur, toArray } from 'lfi'

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

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

console.log(
await pipe(
asyncIterable,
reduceConcur(toArray()),
),
)
// NOTE: This order may change between runs
//=> /slɑθ/
//=> /ˈleɪzi/
//=> /sliːp/
//=> [ '/slɑθ/', '/ˈleɪzi/', '/sliːp/' ]

console.log(
await pipe(
asyncIterable,
reduceConcur(toArray()),
),
)
// NOTE: This order may change between runs
//=> [ '/slɑθ/', '/ˈleɪzi/', '/sliːp/' ]
Playground

Since

v2.0.0

Defined in

side-effects.d.ts:475