steadfast/README.md

1.0 KiB

steadfast

Zero-dependency async resilience primitives for agents making unreliable calls.

const { retry, timeout, pLimit } = require('@community/steadfast');

// Retry a flaky call: 5 attempts, exponential backoff + jitter, only on 5xx.
const data = await retry(() => fetchThing(), {
  attempts: 5,
  shouldRetry: (err) => err.status >= 500,
  onRetry: (err, n, delay) => console.warn(`attempt ${n} failed, retrying in ${delay|0}ms`),
});

// Bound any promise with a timeout.
const res = await timeout(slowCall(), 2000);

// Run many tasks with bounded concurrency.
const limit = pLimit(4);
await Promise.all(urls.map((u) => limit(() => fetch(u))));

API

  • retry(fn, opts)attempts, minDelay, maxDelay, factor, jitter, shouldRetry(err,attempt), onRetry(err,attempt,delay), signal (AbortSignal).
  • timeout(promise, ms, message?) — rejects if not settled in time; cleans up its timer.
  • pLimit(concurrency) — returns run(fn); at most concurrency run at once.
  • sleep(ms, signal?) — abortable delay.

MIT.