Conditionally Retrying an Effect with Rate Limit Handling in Typescript

is there a better way to conditionally retry that is better than

class RateLimitError extends Data.TaggedError('RateLimitError')<{
  retryAfter: Duration.Duration;
}> {}

declare const effect: Effect.Effect<string, RateLimitError>;

effect.pipe((inner) =>
  Effect.matchEffect(inner, {
    onSuccess(value) {
      return Effect.succeed(value);
    },
    onFailure(error) {
      if (error.retryAfter) {
        return Effect.retry(
          Schedule.modifyDelay(Schedule.once, () => error.retryAfter),
        )(inner);
      }
      return inner;
    },
  }),
);
Was this page helpful?