IntermediateCoroutines
async and await
Get a result from a concurrent coroutine.
Dependency required:
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0")kotlin
import kotlinx.coroutines.*
fun main() = runBlocking {
val result = async {
delay(500)
10 + 20
}
println(result.await())
}Output
30
Explanation
async returns a Deferred. await() suspends until the result is ready, then returns it.