Ryan Zidago
01/14/2022, 5:46 PMfun someFunction() = runBlocking {
greet()
}
suspend fun greet() = coroutineScope {
launch {
delay(1_000)
println("world!")
}
println("hello")
}
I will see both "hello" and "world!" printed, but the code is blocked until greet
is done.
However if Iaunch greet
inside a coroutine:
fun someFunction() = runBlocking {
launch {
greet()
}
}
suspend fun greet() = coroutineScope {
launch {
delay(1_000)
println("world!")
}
println("hello")
}
the code is non blocking anymore but I can't see any messages printed to the console 🤔Joffrey
01/14/2022, 5:52 PMlaunch
in your greet()
function should not compile without coroutine scope 🤔Ryan Zidago
01/14/2022, 6:03 PMcoroutine
scope.
I just edited my post.Joffrey
01/14/2022, 6:13 PMrunBlocking
waits for child coroutines to finish before returning. You can see it run here: https://pl.kotl.in/DHI3W7ifV
If you're not seeing this result, you should probably share the actual code you're usingRyan Zidago
01/14/2022, 6:25 PM