Effect.ts: Absence as First-Class

Error HandlingLesson 8 of 51

8. HOW #3: Timeout Strategy

Set a deadline for absence resolution

Code Example
const slow = Effect.gen(function* () {
  yield* Effect.sleep("5 seconds");
  return "Done eventually";
});

// HOW: Give up after 2 seconds
const withTimeout = slow.pipe(
  Effect.timeout("2 seconds")
);

// Result: TimeoutException after 2s
Interactive Example
const fast = Effect.gen(function* () {
      yield* Effect.sleep('50 millis');
      return 'Completed in time!';
});
const withTimeout = fast.pipe(
      Effect.timeout('2 seconds')
);
const result = await Effect.runPromise(withTimeout);
return `Timeout strategy!

Task took: 50ms
Timeout: 2000ms
Result: "${result}"

✓ Completed before timeout!`;
Explanation

Timeout: another way to handle unavailability.

Traditional approach: wait indefinitely, hoping data becomes available. Effect types unavailability with time constraints:

  • Data is unavailable (slow operation)
  • Describe: "Give up if it takes longer than 2 seconds"
  • No assumptions: we explicitly model "absence might be permanent, set a deadline"

We're composing HOW strategies - timeout is just another way to handle absence!


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