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

lhwdev

06/23/2021, 5:30 AM
Is there anything like 'daemon coroutine' in
coroutineScope
that will be cancelled if other things in it are completed? Normally tasks running in
launch
in it will cause
coroutineScope
not to return.
e

ephemient

06/23/2021, 6:12 AM
one possibility:
Copy code
runBlocking {
    val job = Job()
    launch(job) {
        while (true) yield()
    }
    launch(job) {
        delay(5_000)
    }

    delay(100)

    job.cancel()
}
// done in 100ms
l

lhwdev

06/23/2021, 6:13 AM
I asked this because I was lazy to write `.cancel()`; However this seems quite good.
Maybe I could use simple try .. finally...
e

ephemient

06/23/2021, 2:45 PM
you shouldn't need try-finally, an exceptional coroutine termination automatically cancels children
8 Views