repeatOrElse

suspend fun <R> repeatOrElse(times: Long = 3, delay: Duration = 500.milliseconds, onError: suspend (Throwable, Long) -> R, block: suspend () -> R): R(source)

Repeats an operation and invokes an error handler if any execution fails.

Return

The result of the last successful execution

Example usage:

val result = repeatOrElse(
times = 5,
onError = { error, attempt -> logError(error, attempt) }
) {
dataService.process()
}

Parameters

times

The number of times to repeat after initial execution (defaults to 3)

delay

The delay between repetitions (defaults to 500 milliseconds)

onError

Error handler that receives the exception and attempt number

block

The suspend operation to repeat

Type Parameters

R

The return type of the suspend operation