Problems › TypeScript › payments
Should this call be retried
The gateway client decides whether a failed call is worth trying again, or whether retrying would only make things worse.
- Retry a 429 and anything in the 500s — those are the bank being busy or broken.
- Never retry anything else; a 400 will fail the same way forever.
- Three attempts is the ceiling, so an attempt number of 3 or more stops.
- The attempt number counts from 1; anything lower is nonsense and stops too.
shouldRetry(status: int, attempt: int) → bool
Where you start
function shouldRetry(status: number, attempt: number): boolean {
}
Worked examples
| Call | Result |
|---|---|
shouldRetry(500, 1) | true |
shouldRetry(429, 2) | true |
shouldRetry(503, 3) | false |
shouldRetry(404, 1) | false |
Hint
Deal with the attempt ceiling first, then the status.
Reference solution in TypeScript
function shouldRetry(status: number, attempt: number): boolean {
if (attempt < 1 || attempt >= 3) return false;
return status === 429 || (status >= 500 && status <= 599);
}