Skip to main content

Function: findAsync()

Returns an async iterable containing the first value of asyncIterable for which fn returns a value awaitable to a truthy value. Otherwise, returns an empty async iterable.

Like Array.prototype.find, but for async iterables.

Example

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

const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`
const getPartsOfSpeech = async word => {
const response = await fetch(`${API_URL}/${word}`)
const [{ meanings }] = await response.json()
return meanings.map(meaning => meaning.partOfSpeech)
}

console.log(
await pipe(
asAsync([`sloth`, `lazy`, `sleep`]),
findAsync(async word => (await getPartsOfSpeech(word)).includes(`verb`)),
orAsync(() => `not found!`),
),
)
//=> sloth

console.log(
await pipe(
asAsync([`sloth`, `lazy`, `sleep`]),
findAsync(async word => (await getPartsOfSpeech(word)).includes(`adverb`)),
orAsync(() => `not found!`),
),
)
//=> not found!
Playground

Since

v0.0.1

Call Signature

findAsync<Value>(fn): (asyncIterable) => AsyncOptional<Value>

Type Parameters

Value

Parameters

fn

(value) => unknown

Returns

Function

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncOptional<Value>

Defined in

filters.d.ts:721

Call Signature

findAsync<Value>(fn, asyncIterable): AsyncOptional<Value>

Type Parameters

Value

Parameters

fn

(value) => unknown

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncOptional<Value>

Defined in

filters.d.ts:721