Skip to main content

Variable: orConcur()

const orConcur: {<Value>(fn): (concurIterable) => Promise<Value>; <Value>(fn, concurIterable): Promise<Value>; }

Defined in: optionals.d.ts:155

Returns a promise that resolves to the only value in concurIterable if it contains exactly one value. Otherwise, returns a promise that resolves to the awaited result of invoking fn.

The promise only rejects if fn throws or rejects. It does not reject if the given concurIterable rejects. Instead this function excludes erroring values when counting the number of values in concurIterable.

Call Signature​

<Value>(fn): (concurIterable) => Promise<Value>

Type Parameters​

Value​

Value

Parameters​

fn​

() => MaybePromiseLike<Value>

Returns​

(concurIterable): Promise<Value>

Parameters​

concurIterable​

ConcurIterable<Value>

Returns​

Promise<Value>

Call Signature​

<Value>(fn, concurIterable): Promise<Value>

Type Parameters​

Value​

Value

Parameters​

fn​

() => MaybePromiseLike<Value>

concurIterable​

ConcurIterable<Value>

Returns​

Promise<Value>

Example​

import { asConcur, findConcur, orConcur, pipe } from 'lfi'

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

const findWordWithPartOfSpeech = partOfSpeech =>
pipe(
asConcur([`sloth`, `lazy`, `sleep`]),
findConcur(async word => {
const response = await fetch(`${API_URL}/${word}`)
const [{ meanings }] = await response.json()
return meanings.some(meaning => meaning.partOfSpeech === partOfSpeech)
}),
orConcur(() => `no ${partOfSpeech}???`),
)

console.log(await findWordWithPartOfSpeech(`noun`))
// NOTE: This word may change between runs
//=> sloth
console.log(await findWordWithPartOfSpeech(`verb`))
// NOTE: This word may change between runs
//=> sloth
console.log(await findWordWithPartOfSpeech(`adjective`))
//=> lazy
console.log(await findWordWithPartOfSpeech(`adverb`))
//=> no adverb???
Playground

Since​

v0.0.2