retry

suspend fun <A> Schedule<Throwable, *>.retry(action: suspend () -> A): A(source)

Retries action using any Throwable 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(source)

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>(source)

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