For flows, we can collect for x seconds by doing ...
# coroutines
a
For flows, we can collect for x seconds by doing
Copy code
withTimeoutOrNull(time) {
  flow.collect { ... }
}
Is there a way to do this in the middle of the flow, such that the function still returns
Flow<T>
?
c
It seems like the documentation says as you have written: https://kotlinlang.org/docs/reference/coroutines/flow.html#flow-cancellation-basics
a
I understand that part. I was wondering for cases where I want to return
Flow<T>
for other calls, as I won’t be collecting values yet
b
you can write simple extension:
Copy code
fun <T> Flow<T>.withTimeout(timeMillis: Long): Flow<T> = flow {
    withTimeoutOrNull(timeMillis) {
        collect { 
            emit(it)
        }
    }
}
in this case you literally do this in the middle of the flow
👍 4
a
cool! totally forgot that you can emit while collecting