Skip to main content

Function: getAsync()

getAsync<Value>(asyncIterable): Promise<Value>

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

Type Parameters

Value

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

Promise<Value>

Example

import { asAsync, findAsync, getAsync, pipe } from 'lfi'

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

const findWordWithPartOfSpeech = partOfSpeech =>
pipe(
asAsync([`sloth`, `lazy`, `sleep`]),
findAsync(async word => {
const response = await fetch(`${API_URL}/${word}`)
const [{ meanings }] = await response.json()
return meanings.some(meaning => meaning.partOfSpeech === partOfSpeech)
}),
getAsync,
)

console.log(await findWordWithPartOfSpeech(`noun`))
//=> sloth
console.log(await findWordWithPartOfSpeech(`verb`))
//=> 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.1

Defined in

optionals.d.ts:230