retry
Runs an effect and, if it fails, decide using the provided policy if the effect should be retried and if so, with how much delay. Returns the result of the effect if if it was successful or re-raises the last error encountered when the schedule ends.
Retries collection of the given flow when an exception occurs in the upstream flow based on a decision by the schedule. This operator is transparent to exceptions that occur in downstream flow and does not retry on exceptions that are thrown to cancel the flow.
See also
for how to build a schedule.
import kotlinx.coroutines.flow.*
import arrow.fx.coroutines.*
suspend fun main(): Unit {
var counter = 0
val flow = flow {
emit(counter)
if (++counter <= 5) throw RuntimeException("Bang!")
}
//sampleStart
val sum = flow.retry(Schedule.recurs(5))
.reduce(Int::plus)
//sampleEnd
println(sum)
}
Content copied to clipboard
Parameters
schedule
the Schedule used for retrying the collection of the flow