Utilities Module

Logic & Validation (async & is)

Resilient asynchronous control flow primitives and rigorous type/value validation guards.

Async Utilities (async)

.retry(fn, options?)

Executes an async function with automatic retry logic, exponential backoff, and error filtering.

typescript
const status = await __sys__.utils.async.retry(
    async () => {
        const res = await fetch("http://api.internal/");
        if (res.status !== 200) throw new Error("Retryable error");
        return res.status;
    },
    { maxAttempts: 5, delay: 100, backoffFactor: 2 }
);

.pool(items, fn, concurrency?)

Processes an array of items in parallel with a limited number of active promises (concurrency control).

typescript
await __sys__.utils.async.pool(imageUrls, downloadImage, 5); // Max 5 parallel downloads

.timeout(fn, ms)

Wraps a promise with a maximum execution time. Throws an error if the timeout is reached.

typescript
const result = await __sys__.utils.async.timeout(() => heavyTask(), 2000);

.attempt(fn)

Executes an async function and returns a discriminated result object. Simplifies flow control by avoiding global try/catch blocks.

typescript
const { ok, value, error } = await __sys__.utils.async.attempt(() => apiCall());

.repeat(fn, ms, signal?)

Executes a function repeatedly with a fixed interval and automatic drift correction.

typescript
__sys__.utils.async.repeat(() => console.log("Tick"), 1000);

.queue()

Creates a sequential task queue where jobs are executed one by one in the order they were added.

typescript
const q = __sys__.utils.async.queue();
q.add(async () => { /* job 1 */ });

.memoize(fn, ttl?)

Caches the results of an async function. Supports request collapsing for identical concurrent calls.

typescript
const cachedFetch = __sys__.utils.async.memoize(fetchData, 5000);

.sleep(ms, signal?)

Suspends execution for a specific duration. Supports AbortSignal for clean cancellation.

.poll(fn, pred)

Repeated condition check

.debounce(fn, ms)

Burst prevention

.throttle(fn, ms)

Frequency limiting

.once(fn)

Single execution guard

Validation Utilities (is)

.email(string)
RFC 5322 Validation
.url(string)
Strict URI Check
.nullish(value)
Type Guard
Primitives Utilities

Learn about core identity and functional helpers.