louiscad
03/21/2019, 5:25 PMwhile (true)
loops because while (isActive)
continues execution and doesn't throw:
@UseExperimental(InternalCoroutinesApi::class)
fun CoroutineContext.checkIsActiveOrCancelNow() {
val job = get(Job)
if (job != null && !job.isActive) throw job.getCancellationException()
}
This is quite useful in loops where I implement retrying.
The alternative to that coroutineContext.checkIsActiveOrCancelNow()
call is to use if (isActive.not()) yield()
, but I find that it doesn't communicate the intent well.
Do you think this should be integrated into kotlinx.coroutines? I'm personally inclined to open an issue there, but I want to know if I'm not alone first.Vsevolod Tolstopyatov [JB]
03/21/2019, 5:26 PMlouiscad
03/21/2019, 5:27 PMlouiscad
03/21/2019, 5:27 PM