close
Skip to main content

@std/async@1.3.0
Built and signed on GitHub Actions

Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score100%
License
MIT
Downloads8,066/wk
Publisheda month ago (1.3.0)

Utilities for asynchronous operations, like delays, debouncing, or pooling

Classes

c

Multiplexes multiple async iterators into a single stream. It currently makes an assumption that the final result (the value returned and not yielded from the iterator) does not matter; if there is any result, it is discarded.

  • add(iterable: AsyncIterable<T>): void

    Add an async iterable to the stream.

  • iterate(): AsyncIterableIterator<T>

    Returns an async iterator of the stream.

c
RetryError(
cause: unknown,
attempts: number
)

Error thrown in retry once the maximum number of failed attempts has been reached.

Functions

f
abortable<T>(
p: Promise<T> | AsyncIterable<T>,
signal: AbortSignal
): Promise<T> | AsyncIterable<T>
2 overloads

Make a Promise abortable with the given signal.

f
allKeyed<T extends Record<PropertyKey, unknown>>(record: PromiseRecord<T>): Promise<T>

Resolves all values in a record of promises in parallel, returning a promise that resolves to a record with the same keys and resolved values.

f
allSettledKeyed<T extends Record<PropertyKey, unknown>>(record: PromiseRecord<T>): Promise<SettledRecord<T>>

Resolves all values in a record of promises in parallel, returning a promise that resolves to a record with the same keys and PromiseSettledResult objects as values.

f
deadline<T>(
p: Promise<T>,
ms: number,
options?: DeadlineOptions
): Promise<T>

Create a promise which will be rejected with DOMException when a given delay is exceeded.

f
debounce<T extends Array<any>>(
fn: (
this: DebouncedFunction<T>,
...args: T
) => void
,
wait: number
): DebouncedFunction<T>

Creates a debounced function that delays the given func by a given wait time in milliseconds. If the method is called again before the timeout expires, the previous call will be aborted.

f
delay(
ms: number,
options?: DelayOptions
): Promise<void>

Resolve a Promise after a given amount of milliseconds.

f
poll<T>(
fn: () => T,
isDone: (result: Awaited<T>) => boolean,
options?: PollOptions
): Promise<Awaited<T>>

Repeatedly calls a function until a condition is met, then returns the result.

f
pooledMap<T, R>(
poolLimit: number,
array: Iterable<T> | AsyncIterable<T>,
iteratorFn: (data: T) => Promise<R>
): AsyncIterableIterator<R>

pooledMap transforms values from an (async) iterable into another async iterable. The transforms are done concurrently, with a max concurrency defined by the poolLimit.

f
retry<T>(
fn: (() => Promise<T>) | (() => T),
options?: RetryOptions
): Promise<T>

Calls the given (possibly asynchronous) function up to maxAttempts times. Retries as long as the given function throws. If the attempts are exhausted, throws a RetryError with cause set to the inner exception.

f
tee<T, N extends number = 2>(
iterable: AsyncIterable<T>,
n?: N
): Tuple<AsyncIterable<T>, N>

Branches the given async iterable into the n branches.

Interfaces

  • signal: AbortSignal

    Signal used to abort the deadline.

I

A debounced function whose execution is delayed by a given wait time in milliseconds. If the function is called again before the timeout expires, the previous call will be aborted.

  • clear(): void

    Clears the debounce timeout and omits calling the debounced function.

  • flush(): void

    Clears the debounce timeout and calls the debounced function immediately.

  • pending: boolean

    Returns a boolean whether a debounce call is pending or not.

I

Options for delay.

  • persistent: boolean

    Indicates whether the process should continue to run as long as the timer exists.

  • signal: AbortSignal

    Signal used to abort the delay.

I

Options for poll.

  • interval: number

    The interval in milliseconds between each poll.

  • signal: AbortSignal

    Signal used to abort the polling.

I

Options for retry.

  • isRetriable: (err: unknown) => boolean

    Callback to determine if an error or other thrown value is retriable.

  • jitter: number

    Amount of jitter to introduce to the time between attempts. This is 1 for full jitter by default.

  • maxAttempts: number

    The maximum amount of attempts until failure.

  • maxTimeout: number

    The maximum milliseconds between attempts.

  • minTimeout: number

    The initial and minimum amount of milliseconds between attempts.

  • multiplier: number

    How much to backoff after each retry.

  • signal: AbortSignal

    An AbortSignal to cancel the retry operation.

Type Aliases

T
PromiseRecord<T extends Record<PropertyKey, unknown>> = [K in keyof T]: PromiseLike<T[K]> | T[K]

A record type where values can be promise-like (thenables) or plain values.

T
SettledRecord<T extends Record<PropertyKey, unknown>> = [K in keyof T]: PromiseSettledResult<T[K]>

A record type where values are PromiseSettledResult objects.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/async

Import symbol

import * as mod from "@std/async";
or

Import directly with a jsr specifier

import * as mod from "jsr:@std/async";