bhylu.com

Advertisements

Kotlin Async and Await

Collect independent results while preserving a deliberate report order.

How it works

async creates a Deferred result in its scope, and await obtains the value or throws its failure. Use it when results need concurrent computation, not as decoration around every function. Structured scopes connect child failure and cancellation to their owner.

Run this Kotlin example

These are local Kotlin/JVM programs. Install a JDK and the compiler using the official Kotlin compiler guide, or use an IDE with Kotlin support. Check java -version and kotlinc -version in a new terminal. The reviewed compiler is Kotlin 2.4.21 with a JVM 21 target, executed on Temurin JDK 25.0.5. Use JDK 21 or newer for this target. Android, Kotlin/JS and Kotlin/Native require separate platform projects and are not the tested environment here.

Save the complete source as async-await.kt in its own learning folder. The package line gives this example a namespace, not a network address. Bhylu has no Kotlin browser runner; do not use the JavaScript or Python editor. Apache, PHP and MySQL are not required.

This example also needs kotlinx-coroutines-core-jvm-1.11.0.jar, the separately versioned JetBrains coroutines library. Obtain that exact artifact from Maven Central, retain its licence and put it beside this source. Merely installing the compiler does not promise that its bundled coroutine JAR has the reviewed version.

On Windows CMD or PowerShell, compile with kotlinc async-await.kt -jvm-target 21 -classpath kotlinx-coroutines-core-jvm-1.11.0.jar -include-runtime -d lesson.jar, then run java -cp "lesson.jar;kotlinx-coroutines-core-jvm-1.11.0.jar" bhylu.lessons.k_async_await.Async_awaitKt. On macOS/Linux use a colon instead of the semicolon between classpath entries. java -jar alone will not add this external library.

Download the complete async-await.kt · Download all source files with hash checks

For the optional collection, save download-examples.mjs in a new learning folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/kotlin/. It refuses an existing bhylu-kotlin-examples folder and installs or executes no Kotlin code. Compile one ordinary lesson at a time by filename.

Example

async-await.kt

package bhylu.lessons.k_async_await

import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.async
import kotlinx.coroutines.Dispatchers


fun main() {
    runBlocking {
        val first = async(Dispatchers.Default) { 3 * 10 }
        val second = async(Dispatchers.Default) { 2 * 10 }
        println(listOf(first.await(), second.await()))
    }
}

Expected result

[30, 20]

The worker scheduling order is unspecified, but awaiting into a fixed list gives a deterministic report. These tiny arithmetic tasks illustrate the contract; they do not establish a performance benefit from concurrency.

For exact language and library contracts, consult the official Kotlin documentation. These fictional fixtures are learning programs, not a deployed hire service.

Advertisements