Sobes.tech
Junior — Senior

Behavior of runBlocking when working with async coroutines

livecode

Task condition

Determine which output will appear in the console after executing the following program:

val coroutineContext = Job() + Dispatchers.Default
val mainScope = CoroutineScope(coroutineContext)

val first = mainScope.async {
    println(1)
    delay(400)
    "First Async"
}

val second = mainScope.async {
    println(2)
    delay(100)
    "Second Async"
}

runBlocking {
    delay(300)
    println(3)
    println("${first.await()} ${second.await()}")
}