Deviopath › TypeScript

Retry with Exponential Backoff

Hard · Async

Write retry(task, options, sleep). task(attempt) returns a promise; attempt starts at 1. sleep(ms) waits (the driver passes a fake one that only records the delays).

- If the task resolves, return { ok: true, value, attempts }.

- If it rejects with an error whose retryable property is false (a TaskError), stop immediately.

- Otherwise, if fewer than options.retries retries have been used, call sleep(delay) and try again. After failed attempt number n the delay is min(maxDelay, baseDelay * factor^(n - 1)).

- When giving up, return { ok: false, error, attempts } where error is the message of the last error.

So retries: 2 allows at most 3 attempts. The driver runRetry(outcomes, options) is already written: outcomes[i] describes attempt i + 1 ("ok:<value>", "fail" or "fatal"), and it adds the recorded delays to your result.

Next: Compose Async Middleware