I have these two suspend functions
suspend fun doSomethingUsefulOne(): Int {
delay(3000L)
return 20
}
suspend fun doSomethingUsefulTwo(): Int {
delay(3000L)
return 10
}
I wanted to run both the functions parallelly so I'm running the below code. In this case I'm getting the result in ~3 seconds
runBlocking {
val a = async { doSomethingUsefulOne() }
val b = async { doSomethingUsefulTwo() }
println(a.await()+b.await())
}
But when I tried to use the below function, I am getting the result after ~6 sec. Why is that so?
runBlocking {
println(async { doSomethingUsefulOne() }.await()+async { doSomethingUsefulTwo() }.await())
}
awaitbefore starting the secondasyncblock.