Skip to main content

Function: getConcur()

getConcur<Value>(concurIterable): Promise<Value>

Returns a promise that resolves to the only value in concurIterable if it contains exactly one value. Otherwise, returns a promise that rejects.

Type Parameters

Value

Parameters

concurIterable

ConcurIterable<Value>

Returns

Promise<Value>

Example

import { asConcur, findConcur, getConcur, 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)
}),
getConcur,
)

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
try {
console.log(await findWordWithPartOfSpeech(`adverb`))
} catch {
console.log(`Oh no! It was empty...`)
}
//=> Oh no! It was empty...
Playground

Since

v0.0.2

Defined in

optionals.d.ts:274