retryForever

suspend fun <T> retryForever(delayBetween: Duration = Duration.ZERO, retryIf: (Throwable) -> Boolean = { true }, block: suspend (attempt: Int) -> T): T

Runs block repeatedly until it succeeds and returns that first successful result.

The attempt number passed to block starts at 1 and saturates at Int.MAX_VALUE. After a failure that retryIf accepts, this function suspends for delayBetween and tries again. A failure that retryIf rejects is rethrown unchanged.

Cancellation: a CancellationException thrown by block or while delaying is rethrown immediately and never passed to retryIf; cancelling the calling coroutine is the only way to stop an endlessly failing loop.

Parameters

delayBetween

the pause between consecutive attempts.

retryIf

returns true when the given failure should be retried.

Throws