async.ts

view source

Declarations
#

9 declarations

AsyncSemaphore
#

async.ts view source

import {AsyncSemaphore} from '@fuzdev/fuz_util/async.js';

Async semaphore for concurrency limiting.

With Infinity permits, acquire() always resolves immediately.

constructor

type new (permits: number): AsyncSemaphore

permits

type number

throws

  • Error - if `permits < 0`

acquire

type (): Promise<void>

returns Promise<void>

release

type (): void

returns void

AsyncStatus
#

create_deferred
#

async.ts view source

<T>(): Deferred<T> import {create_deferred} from '@fuzdev/fuz_util/async.js';

Creates an object with a promise and its resolve/reject handlers.

returns

Deferred<T>

generics

create_deferred<T>
T

Deferred
#

async.ts view source

Deferred<T> import type {Deferred} from '@fuzdev/fuz_util/async.js';

A deferred object with a promise and its resolve/reject handlers.

generics

Deferred<T>
T

promise

type Promise<T>

resolve

type (value: T) => void

reject

type (reason: any) => void

each_concurrent
#

async.ts view source

<T>(items: Iterable<T>, concurrency: number, fn: (item: T, index: number) => void | Promise<void>, signal?: AbortSignal | undefined): Promise<...> import {each_concurrent} from '@fuzdev/fuz_util/async.js';

Runs a function on each item with controlled concurrency. Like map_concurrent but doesn't collect results (more efficient for side effects).

items

type Iterable<T>

concurrency

maximum number of concurrent operations

type number

fn

type (item: T, index: number) => void | Promise<void>

signal?

optional AbortSignal to cancel processing

type AbortSignal | undefined
optional

returns

Promise<void>

generics

each_concurrent<T>
T

throws

  • Error - if `concurrency < 1`

examples

await each_concurrent( file_paths, 5, // max 5 concurrent deletions async (path) => { await unlink(path); }, );

is_promise
#

async.ts view source

(value: unknown): value is Promise<unknown> import {is_promise} from '@fuzdev/fuz_util/async.js';

Checks if value is a Promise (or thenable).

value

type unknown

returns

boolean

map_concurrent
#

async.ts view source

<T, R>(items: Iterable<T>, concurrency: number, fn: (item: T, index: number) => R | Promise<R>, signal?: AbortSignal | undefined): Promise<...> import {map_concurrent} from '@fuzdev/fuz_util/async.js';

Maps over items with controlled concurrency, preserving input order.

items

type Iterable<T>

concurrency

maximum number of concurrent operations

type number

fn

type (item: T, index: number) => R | Promise<R>

signal?

optional AbortSignal to cancel processing

type AbortSignal | undefined
optional

returns

Promise<R[]>

array of results in same order as input

generics

map_concurrent<T, R>
T
R

throws

  • Error - if `concurrency < 1`

examples

const results = await map_concurrent( file_paths, 5, // max 5 concurrent reads async (path) => readFile(path, 'utf8'), );

map_concurrent_settled
#

async.ts view source

<T, R>(items: Iterable<T>, concurrency: number, fn: (item: T, index: number) => R | Promise<R>, signal?: AbortSignal | undefined): Promise<...> import {map_concurrent_settled} from '@fuzdev/fuz_util/async.js';

Like map_concurrent but collects all results/errors instead of failing fast. Returns an array of settlement objects matching the Promise.allSettled pattern.

On abort, resolves with partial results: completed items keep their real settlements, in-flight and un-started items are settled as rejected with the abort reason.

items

type Iterable<T>

concurrency

maximum number of concurrent operations

type number

fn

type (item: T, index: number) => R | Promise<R>

signal?

optional AbortSignal to cancel processing

type AbortSignal | undefined
optional

returns

Promise<PromiseSettledResult<R>[]>

array of PromiseSettledResult objects in input order

generics

map_concurrent_settled<T, R>
T
R

throws

  • Error - if `concurrency < 1`

examples

const results = await map_concurrent_settled(urls, 5, fetch); for (const [i, result] of results.entries()) { if (result.status === 'fulfilled') { console.log(`${urls[i]}: ${result.value.status}`); } else { console.error(`${urls[i]}: ${result.reason}`); } }

wait
#

async.ts view source

(duration?: number): Promise<void> import {wait} from '@fuzdev/fuz_util/async.js';

Waits for the given duration before resolving.

duration

type number
default 0

returns

Promise<void>

Imported by
#