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

myanmarking

08/22/2019, 1:07 PM
someAsyncJobHere not to cancel the outer coroutine. how would i define such function ?
k

kingsley

08/22/2019, 1:12 PM
You can use launch/async or most of the other existing coroutine builders depending on which one suits your needs:
Copy code
fun CoroutineScope.someAsyncJobHere() = launch {
  // some code
}
This function returns a
Job
that you can then cancel (or not) later
m

myanmarking

08/22/2019, 1:13 PM
yes, but if i cancel the job, won't it cancel the rest of the execution inside main.launch ?
please check edited code sample, i don't want it to cancel suspendFunc3
Copy code
fun startStepsSync(): CoroutineScope {
        val scope = CoroutineScope(Dispatchers.Main)
        scope.launch { 
            // something here
        }
        return scope
    }
would canceling this job also cancel the mainScope?
l

louiscad

08/22/2019, 1:34 PM
Just test it, it'll improve your understanding. Spoiler: canceling a child coroutine doesn't cancel the parent. Crashing a child coroutine does cancel the parent (a non
CancellationException
).
m

myanmarking

08/22/2019, 1:36 PM
ya, thanks
in that case, i don't need the inner scope at all, right ?
l

louiscad

08/22/2019, 1:41 PM
If you don't seem to need it, then you probably don't.
s

streetsofboston

08/22/2019, 2:24 PM
@myanmarking https://medium.com/the-kotlin-chronicle/coroutine-exceptions-3378f51a7d33 and look for the “CancellationException” section.
3 Views