Hi, any idea why this function requires `return` ...
# getting-started
a
Hi, any idea why this function requires
return
statement in the end even if we are throwing exception
onFailure
Copy code
fun<T> withRetryOnRateLimit(numOfRetries: Int = 10, func: () -> T): T {
    repeat(numOfRetries) {
        runCatching(func)
                .onSuccess { return it }
                .onFailure { throw it }
    }
}
s
The throwing happens inside a callback block. You still need to return a T in the withRetryOnRateLimit method itself, as you are declaring that.
1
s
The compiler doesn’t know that it is guaranteed that either onSuccess or onFailure will get called (all it knows is that each will be called at most once). Which also makes me wonder what’s the point of the loop here? The first iteration is guaranteed to also be the last.
2
Also, I’m assuming that this is a simplified version of the code, because in its current form, it’s functionally identical to
return func()