Skip to main content

Function: orAsync()

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.

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???
Playground

Since

v0.0.1

Call Signature

orAsync<Value>(fn): (asyncIterable) => Promise<Value>

Type Parameters

Value

Parameters

fn

() => MaybePromiseLike<Value>

Returns

Function

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

Promise<Value>

Defined in

optionals.d.ts:105

Call Signature

orAsync<Value>(fn, asyncIterable): Promise<Value>

Type Parameters

Value

Parameters

fn

() => MaybePromiseLike<Value>

asyncIterable

AsyncIterable<Value, any, any>

Returns

Promise<Value>

Defined in

optionals.d.ts:108