Sobes.tech
Junior — Senior

Determining the output of asynchronous coroutines in Kotlin

livecode

Task condition

Below is an example of Kotlin code using coroutines. Determine which messages will be printed to the console when it runs.

fun main() = runBlocking {
    val jobA = async { outputSecond() }
    val jobB = async { outputFirst() }

    jobA.await()
    jobB.await()
}

suspend fun outputFirst() {
    for (index in 0..10) {
        delay(5)
        println("printFirst - $index")
    }
}

suspend fun outputSecond() {
    for (index in 0..10) {
        delay(10)
        println("printSecond - $index")
    }
}