Pim
02/20/2025, 10:50 AMasync
and cancellations. Lets say I have:
withContext(dispatcher) {
val job = async { while(true) { delay(150) } }
withTimeoutOrNull(200) { job.await() }
}
Id think this would start some async job, and then wait for its completion for 200ms. If the job takes longer than 200ms, it cancels the await and the async job.
Right now it looks like nothing times out. Am I missing something?Dmitry Khalanskiy [JB]
02/20/2025, 10:53 AM{ job.await() }
gets cancelled in this case, that is, the process of waiting for the job
to complete. Nothing cancels the job
itself.
Related issue: https://github.com/Kotlin/kotlinx.coroutines/issues/4329Pim
02/20/2025, 10:57 AMjob
myself on receiving null from the withTimeoutOrNull
. Or would there be a better/cleaner way of achieving what I want?Dmitry Khalanskiy [JB]
02/20/2025, 11:00 AMOr would there be a better/cleaner way of achieving what I want?This depends entirely on what you want! How about this?
withContext(dispatcher) {
val job = async {
withTimeoutOrNull(200) {
while(true) { delay(150) }
}
}
job.await()
}
Pim
02/20/2025, 11:01 AMuli
02/24/2025, 10:50 AMwithTimeoutOrNull(200) {
withContext(dispatcher) {
while(true) { delay(150) }
}
}