Make a flaky call succeed on its own
Add retry with backoff so transient 429s and 5xxs recover without your code noticing.
Task
An API you depend on returns the occasional 429 or 503 that clears on its
own. You do not want every call site wrapped in a try/catch and a sleep.
Example
One way: declare a retry policy on the stitch and let the caller just await the
value.
import { } from 'stitchapi';
const = ({
: 'https://api.example.com',
: '/users/{id}',
: {
: 3,
: [429, 503],
: 'expo-jitter',
: true,
},
});
// A transient 429 or 503 is retried up to twice; the caller just gets the value.
const = await ({ : { : 1 } });How it works
attempts is the total number of tries including the first, so 3 means up
to two retries. on is the set of status codes that trigger a retry.
backoff: 'expo-jitter' doubles the wait each attempt and spreads it randomly to
avoid thundering herds. respectRetryAfter honors a Retry-After header when the
server tells you when to come back. Each retry rides the
event stream as a progress event (phase
retry), so you can watch it happen.
When the dependency is not just flaky but failing, compose retry with a
timeout and circuit breaker.
See also
Loop a cursor API into one array
Follow nextCursor across every page and aggregate the items — no manual while-loop, cursor bookkeeping, or concat.
Keep a failing dependency from taking you down
Compose timeout, retry, and a circuit breaker so a degraded upstream fails fast instead of hanging every caller.