retryIf

suspend fun <R> retryIf(retries: Long = 3, delay: Duration = 1.seconds, shouldRetry: (Throwable) -> Boolean, block: suspend () -> R): R(source)

Executes a suspend operation with retry using a custom predicate to decide retries.

Return

The result of the successful execution

Parameters

retries

The maximum number of retry attempts (defaults to 3)

delay

The delay between retry attempts (defaults to 1 second)

shouldRetry

Predicate that receives the exception and returns true to retry

block

The suspend operation to execute

Type Parameters

R

The return type of the suspend operation

Throws

if shouldRetry returns false or all retries are exhausted

Example usage:

val response = retryIf(
retries = 5,
shouldRetry = { it is TimeoutException || it is IOException }
) {
httpClient.get("/api/data")
}