Hello, is there a something like `withTimeout(time...
# coroutines
r
Hello, is there a something like
withTimeout(time) { operation }
but without canceling the operation? What I want to achieve: If an operation takes longer than
N
millis I want to have a callback about this and I want the operation to continue working. Something like:
Copy code
withTime(
    time = 500L, 
    block = { operation }, 
    onTimeOver = {  }
)
c
s
What about wrapping the withTimeout in a try catch block? Catch the CancellationException (and call your callback from there).
r
@streetsofboston tried this already. But the operation(network call) inside
withTimeout
is getting cancelled when timeout passes and I lose my results
u
sounds like you want multiple emits, ie. Flow
merge(yourAction, timer(x))
c
what about
launch { delay(500); callTimer()}
r
Tried something like this:
Copy code
launch {
    val job = launch {
        // doing some long operation
    }.apply {  start() }
    delay(N)
    if (!job.isCompleted) {
        // operation took longer than N millis. 
    }
}
And looks like it’s what I wanted. Thanks