Skip to main content

Function: forEachAsync()

Returns a promise that resolves when fn has been applied to each value of asyncIterable and the result of each application has been awaited.

The result of applying fn to a value is awaited before moving on to the next value.

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

Example

import { asAsync, forEachAsync, mapAsync, pipe } from 'lfi'

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

console.log(
await pipe(
asAsync([`sloth`, `lazy`, `sleep`]),
mapAsync(async word => {
const response = await fetch(`${API_URL}/${word}`)
return (await response.json())[0].phonetic
}),
forEachAsync(console.log),
),
)
//=> /slɑθ/
//=> /ˈleɪzi/
//=> /sliːp/
Playground

Since

v0.0.1

Call Signature

forEachAsync<Value>(fn): (asyncIterable) => Promise<void>

Type Parameters

Value

Parameters

fn

(value) => unknown

Returns

Function

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

Promise<void>

Defined in

side-effects.d.ts:210

Call Signature

forEachAsync<Value>(fn, asyncIterable): Promise<void>

Type Parameters

Value

Parameters

fn

(value) => unknown

asyncIterable

AsyncIterable<Value, any, any>

Returns

Promise<void>

Defined in

side-effects.d.ts:213