I find myself needing something like this for use ...
# coroutines
l
I find myself needing something like this for use in
while (true)
loops because
while (isActive)
continues execution and doesn't throw:
Copy code
@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.
👌 1
v
l
Perfect! Thanks for that 🙂
Can't wait for it BTW 🙂