Variable: orAsync()
const
orAsync: {<Value
>(fn
): (asyncIterable
) =>Promise
<Value
>; <Value
>(fn
,asyncIterable
):Promise
<Value
>; }
Defined in: optionals.d.ts:104
Returns a promise that resolves to the only value in asyncIterable
if it
contains exactly one value. Otherwise, returns a promise that resolves to
the awaited result of invoking fn
.
Call Signature
<
Value
>(fn
): (asyncIterable
) =>Promise
<Value
>
Type Parameters
Value
Value
Parameters
fn
() => MaybePromiseLike
<Value
>
Returns
(
asyncIterable
):Promise
<Value
>
Parameters
asyncIterable
AsyncIterable
<Value
>
Returns
Promise
<Value
>
Call Signature
<
Value
>(fn
,asyncIterable
):Promise
<Value
>
Type Parameters
Value
Value
Parameters
fn
() => MaybePromiseLike
<Value
>
asyncIterable
AsyncIterable
<Value
>
Returns
Promise
<Value
>
Example
import { asAsync, findAsync, orAsync, 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)
}),
orAsync(() => `no ${partOfSpeech}???`),
)
console.log(await findWordWithPartOfSpeech(`noun`))
//=> sloth
console.log(await findWordWithPartOfSpeech(`verb`))
//=> sloth
console.log(await findWordWithPartOfSpeech(`adjective`))
//=> lazy
console.log(await findWordWithPartOfSpeech(`adverb`))
//=> no adverb???
Since
v0.0.1