Kotlin 2 · Coroutines

Run work in parallel

async starts work that returns a value, and await gets it. Both calls below run at the same time.

suspend fun total(): Int = coroutineScope {
    val a = async { fetchA() }
    val b = async { fetchB() }
    a.await() + b.await()
}

Official Kotlin 2 documentation

More coroutines commands

All coroutines commands · Kotlin 2 cheatsheet

Was this page helpful?