Repetition is a common requirement when working with effects in software development. It allows us to perform an effect multiple times according to a specific repetition policy.
repeat
The Effect.repeat function returns a new effect that repeats the given effect according to a specified schedule or until the first failure.
Example (Repeating a Successful Effect)
Example (Handling Failures in Repetition)
Skipping First Execution
If you want to avoid the first execution and only run the action according to a schedule, you can use Effect.schedule. This allows the effect to skip the initial run and follow the defined repeat policy.
Example (Skipping First Execution)
repeatN
The repeatN function returns a new effect that repeats the specified effect a given number of times or until the first failure. The repeats are in addition to the initial execution, so Effect.repeatN(action, 1) executes action once initially and then repeats it one additional time if it succeeds.
Example (Repeating an Action Multiple Times)
repeatOrElse
The repeatOrElse function returns a new effect that repeats the specified effect according to the given schedule or until the first failure.
When a failure occurs, the failure value and schedule output are passed to a specified handler.
Scheduled recurrences are in addition to the initial execution, so Effect.repeat(action, Schedule.once) executes action once initially and then repeats it an additional time if it succeeds.
Example (Handling Failure During Repeats)
Repeating Based on a Condition
You can control the repetition of an effect by a condition using either a while or until option, allowing for dynamic control based on runtime outcomes.