https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

louiscad

03/21/2019, 5:25 PM
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

Vsevolod Tolstopyatov [JB]

03/21/2019, 5:26 PM
l

louiscad

03/21/2019, 5:27 PM
Perfect! Thanks for that 🙂
Can't wait for it BTW 🙂
3 Views