Getting Started
Install retry and run your first retry policy.
Installation
npm install @zap-studio/retryyarn add @zap-studio/retrypnpm add @zap-studio/retrybun add @zap-studio/retrydeno add jsr:@zap-studio/retryQuick Start
Use a built-in policy and run work through policy.run(...).
import { ExponentialBackoff } from "@zap-studio/retry/exponential-backoff";
import { $fetch } from "@zap-studio/fetch";
const policy = new ExponentialBackoff({
maxAttempts: 5,
baseDelayMs: 100,
maxDelayMs: 2_000,
});
const result = await policy.run(async () => {
const response = await $fetch("https://api.example.com/users", {
throwOnFetchError: true,
});
return await response.json();
});By default, exhaustion throws RetryError.
Use throwOnExhausted: false when the caller should receive a result object instead:
const result = await policy.run(fetchUsers, {
throwOnExhausted: false,
});
if (!result.ok) {
console.error(result.attempts);
console.error(result.error.lastError);
} else {
console.log(result.value);
}Edit on GitHub
Last updated on