Effect.ts: Absence as First-Class

Error HandlingLesson 7 of 51

7. HOW #2: Retry Strategy

Another HOW - keep trying until the absent value appears

Code Example
let attempts = 0;

const flaky = Effect.gen(function* () {
  attempts++;
  if (attempts < 3) {
    yield* Effect.fail("Still broken");
  }
  return "Finally worked!";
});

// HOW: Retry up to 5 times
const withRetry = flaky.pipe(
  Effect.retry({ times: 5 })
);

// Attempts 1 & 2 fail, attempt 3 succeeds
Interactive Example
let attempts = 0;
const flaky = Effect.gen(function* () {
      attempts++;
      if (attempts < 3) {
              yield* Effect.fail('Still broken');
      }
      return 'Finally worked!';
});
const withRetry = flaky.pipe(
      Effect.retry({ times: 5 })
);
const result = await Effect.runPromise(withRetry);
return `Retry in action!

Attempt 1: ✗ Failed
Attempt 2: ✗ Failed
Attempt 3: ✓ ${result}

Total attempts: ${attempts}`;
Explanation

Retry: describing unavailability with multiple attempts.

Traditional approach assumes data is available, or fails once and gives up. Effect types the unavailability with retry semantics:

  • Data is unavailable (flaky operation)
  • Describe: "Try up to 5 times to make it available"
  • No assumptions: we explicitly model "it might not be available, so try again"

We're describing HOW to resolve unavailability through persistence, not assuming the data will be there.


Part 7 of 51 in the Effect.ts Absence Modeling series