rocketraman
08/30/2018, 9:05 PMretry
function that takes an action
lambda. Is there a way for me to define this function in such a way that the action
lambda can be either suspending or non-suspending depending on the caller?louiscad
08/30/2018, 9:26 PMsuspend
lambda can not suspend. The contrary is not possible, unless you want to go down the dark route of runBlocking
rocketraman
08/30/2018, 9:30 PMretry
is a peculiar case, because if I make the lambda suspendable, then the tryAction
function I call it in with all of the error handling logic has to be suspending as well. But if I make that suspending, then the retry
function has to be suspending.fun <T> retry(maxAttempts: Int = 3,
delay: Long = 1000,
action: () -> T): T = tryAction(0, maxAttempts, delay, action)
fun <T> tryAction(currentAttempt: Int, maxAttempts: Int, delay: Long, action: () -> T): T {
return try {
action.invoke()
} catch (e: Exception) {
if (currentAttempt + 1 < maxAttempts) {
if(delay > 0) try { Thread.sleep(delay) } catch (e: InterruptedException) { /* ignore */ }
tryAction(currentAttempt + 1, maxAttempts, delay, action)
} else {
throw e
}
}
}
delay
there instead of sleep also), and then have a retryBlocking
wrapper for non-suspending callers.elizarov
08/30/2018, 9:38 PM