async.ts

Declarations
#

9 declarations

view source

AsyncSemaphore
#

async.ts view source

Async semaphore for concurrency limiting.

With Infinity permits, acquire() always resolves immediately.

constructor

type new (permits: number): AsyncSemaphore

permits
type number

acquire

type (): Promise<void>

returns Promise<void>

release

type (): void

returns void

AsyncStatus
#

create_deferred
#

async.ts view source

<T>(): Deferred<T>

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

returns

Deferred<T>

Deferred
#

async.ts view source

Deferred<T>

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

generics

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<...>

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

items

items to process

type Iterable<T>

concurrency

maximum number of concurrent operations

type number

fn

function to apply to each item

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

signal?

optional AbortSignal to cancel processing

type AbortSignal | undefined
optional

returns

Promise<void>

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>

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<...>

Maps over items with controlled concurrency, preserving input order.

items

items to process

type Iterable<T>

concurrency

maximum number of concurrent operations

type number

fn

function to apply to each item

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

signal?

optional AbortSignal to cancel processing

type AbortSignal | undefined
optional

returns

Promise<R[]>

promise resolving to array of results in same order as input

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<...>

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

items to process

type Iterable<T>

concurrency

maximum number of concurrent operations

type number

fn

function to apply to each item

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

signal?

optional AbortSignal to cancel processing

type AbortSignal | undefined
optional

returns

Promise<PromiseSettledResult<R>[]>

promise resolving to array of PromiseSettledResult objects in input order

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>

Waits for the given duration before resolving.

duration

type number
default 0

returns

Promise<void>

Imported by
#