Lesson 06 of 6 in Effect, one function at a time

Try again with Effect.retry

Run the effect again after a failure.

Some failures go away if you try again. Effect.retry runs the effect again after a failure, and a schedule decides how long to wait between attempts and when to stop.

import { Effect, Schedule } from "effect";

let attempts = 0;

const flaky = Effect.sync(() => {
	attempts++;
}).pipe(Effect.andThen(Effect.fail("unavailable" as const)));

const schedule = Schedule.exponential(100).pipe(
	Schedule.setInputType<"unavailable">(),
	Schedule.upTo({ times: 3 }),
);

export const retried = Effect.retry(flaky, { schedule });

Three things to notice:

Retry only the failures that are worth another try. A rejected request with bad input will fail the same way every time, so it should not be retried.