Why `Job::join` does not resume when coroutine is ...
# coroutines
m
Why
Job::join
does not resume when coroutine is just done? The documentation suggests that it should, but I cannot make in resume any other way but by crashing or cancelling a coroutine.
Copy code
import kotlinx.coroutines.*

fun main(): Unit = runBlocking {
    val job = Job()
    launch(job) {
        delay(200)
    }
    job.join()
}
// Runs forever
👍🏽 1
n
launch
creates a new child
Job
, which it gives you as the return value. The child
Job
is finishing but the parent that you create explicitly is never finishing. Try just joining on the
Job
that
launch
returns or call
complete
on the
Job
you created.
e
the job you explicitly created isn't complete just because one child is complete. you can still launch another coroutine in it, for example.
l
You broke structured concurrency, that's why. Tip: don't inject jobs into coroutines manually to avoid that. You probably want to use
coroutineScope { }
or call
join()
on the
Job
instance returned by the
launch { }
call.
m
That makes sense, thank you.