retry

inline suspend fun <E : Throwable, A> Schedule<E, *>.retry(noinline action: suspend () -> A): A
suspend fun <E : Throwable, A> Schedule<E, *>.retry(exceptionClass: KClass<E>, action: suspend () -> A): A

Retries action using any E that occurred as the input to the Schedule. It will throw the last exception if the Schedule is exhausted, and ignores the output of the Schedule.


inline suspend fun <Error, Result, Output> Raise<Error>.retry(schedule: Schedule<Error, Output>, action: Raise<Error>.() -> Result): Result

Retries action using any Error that occurred as the input to the Schedule. It will return the last Error if the Schedule is exhausted, and ignores the output of the Schedule.


fun <A, B> Flow<A>.retry(schedule: Schedule<Throwable, B>): Flow<A>

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.

Parameters

schedule
  • the Schedule used for retrying the collection of the flow

See also

for how to build a schedule.

import arrow.resilience.*
import kotlinx.coroutines.flow.*

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)
}