Skip to main content

Class: AsyncUtils

Defined in: utils/AsyncUtils.ts:5

Utility methods for running large CPU workloads on the main thread without blocking the UI, by cooperatively yielding to the event loop

Constructors

Constructor

new AsyncUtils(): AsyncUtils

Returns

AsyncUtils

Methods

createYielder()

static createYielder(options?): () => Promise<void>

Defined in: utils/AsyncUtils.ts:50

Creates a time-sliced yielder for loops whose iterations are too coarse or heterogeneous for AsyncUtils.forEach (e.g. nested loops).

Call the returned function at safe points inside a loop: it yields to the event loop and re-checks the abort signal once the time budget since the last yield is exceeded, and is otherwise a cheap no-op.

Parameters

options?

Optional abort signal and time (ms) to run before yielding (yieldMs)

signal?

AbortSignal

yieldMs?

number

Returns

() => Promise<void>


forEach()

static forEach<T>(items, callback, options?): Promise<void>

Defined in: utils/AsyncUtils.ts:80

Invokes callback(item, index) for every element of items, yielding to the event loop whenever the time budget is exceeded so the UI stays responsive.

The abort signal is checked up front and after every yield, preserving fail-fast cancellation: an abort rejects with the signal's reason and no further iterations run.

Type Parameters

T

T

Parameters

items

ArrayLike<T>

The array-like collection to iterate over

callback

(item, index) => void

Synchronous work to perform for each item and its index

options?

Optional abort signal, time (ms) to run before yielding (yieldMs), and the number of iterations between budget checks (checkEvery)

checkEvery?

number

signal?

AbortSignal

yieldMs?

number

Returns

Promise<void>


yield()

static yield(): Promise<void>

Defined in: utils/AsyncUtils.ts:20

Yields control back to the event loop, allowing the browser to paint and process input before resuming.

Uses the native scheduler.yield() when available, otherwise falls back to a shared MessageChannel (a macrotask without setTimeout's clamping).

Returns

Promise<void>