Skip to main content
On this page

@std/async

Overview Jump to heading

Provide helpers with asynchronous tasks like delays, debouncing, retrying, or pooling.

import { delay } from "@std/async/delay";

await delay(100); // waits for 100 milliseconds

Add to your project Jump to heading

deno add jsr:@std/async

See all symbols in @std/async on

What is async? Jump to heading

Asynchronous programming lets your program start work now and finish it later without blocking the main thread. In JavaScript this typically means Promises, async/await, timers, and async iterables. Async code improves responsiveness and throughput by overlapping I/O (network, disk, timers) and controlling concurrency.

Why use @std/async? Jump to heading

This package gives you small, focused utilities that solve common async problems so you don’t have to re‑implement them:

  • delay and deadline for scheduling and timeouts (with cancellation via AbortSignal).
  • debounce (and throttle in unstable) to control call frequency for UI/CLI events.
  • retry with backoff/jitter for flaky operations like HTTP requests.
  • pooledMap to cap concurrency when transforming streams of work.
  • tee/MuxAsyncIterator to branch or merge async iterables.

These helpers are tiny, predictable, and work across Deno, Node.js, Bun, Workers, and the browser.

Examples Jump to heading

Debounce rapid events (e.g., typeahead):

import { debounce } from "@std/async/debounce";

const search = debounce(async (q: string) => {
  // fetch results after the user pauses typing
}, 200);

// call many times; only the last call after 200ms runs
search("d");
search("de");
search("deno");

Retry with exponential backoff and jitter:

import { retry } from "@std/async/retry";

const data = await retry(() =>
  fetch("/api").then((r) => {
    if (!r.ok) throw new Error("bad status");
    return r.json();
  }), {
  maxAttempts: 5,
  minTimeout: 100,
  multiplier: 2,
  jitter: 0.2,
});

Limit concurrency with pooledMap:

import { pooledMap } from "@std/async/pool";

const urls = ["/a", "/b", "/c", "/d"]; // many more in practice
for await (const res of pooledMap(3, urls, (u) => fetch(u))) {
  // at most 3 fetches in flight; results arrive as they complete
}

Deadlines and cancellation:

import { deadline } from "@std/async/deadline";

await deadline(fetch("/slow"), 1_000); // throws DOMException("TimeoutError") after 1s

Did you find what you needed?

Privacy policy