someAsyncJobHere not to cancel the outer coroutine...
# coroutines
m
someAsyncJobHere not to cancel the outer coroutine. how would i define such function ?
k
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
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
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
ya, thanks
in that case, i don't need the inner scope at all, right ?
l
If you don't seem to need it, then you probably don't.
s
@myanmarking https://medium.com/the-kotlin-chronicle/coroutine-exceptions-3378f51a7d33 and look for the “CancellationException” section.