Async coroutine builder helps you run tasks in the background without stopping your main program. It lets you start work now and get the result later.
Async coroutine builder in Kotlin
val deferred = async { // some suspend function or long running code } val result = deferred.await()
async starts a coroutine that returns a Deferred object.
You use await() on the Deferred to get the result when ready.
val deferred = async { delay(1000L) "Hello" } val result = deferred.await()
val deferred1 = async { 10 * 10 } val deferred2 = async { 20 + 30 } val sum = deferred1.await() + deferred2.await()
This program starts an async coroutine that waits half a second and returns "Kotlin". Meanwhile, it prints a message. Then it waits for the result and prints it.
import kotlinx.coroutines.* fun main() = runBlocking { val deferred = async { delay(500L) "Kotlin" } println("Waiting for result...") val result = deferred.await() println("Result: $result") }
Use async inside a coroutine scope like runBlocking or launch.
Calling await() suspends the current coroutine until the result is ready.
Don't forget to import kotlinx.coroutines.* to use async and await.
async starts a background task and returns a Deferred result.
Use await() to get the result when it is ready, without blocking the whole program.
This helps keep your app responsive while doing work in the background.
Practice
async coroutine builder do in Kotlin?Solution
Step 1: Understand async behavior
Theasyncbuilder starts a coroutine that runs in the background without blocking the main thread.Step 2: Recognize the return type
It returns aDeferredobject which holds the future result of the background task.Final Answer:
Starts a background task and returns a Deferred result -> Option AQuick Check:
async = background task + Deferred [OK]
- Thinking async blocks the main thread
- Confusing async with launch which returns Job
- Assuming async runs only on main thread
Solution
Step 1: Identify correct async syntax
Theasyncbuilder is called with a lambda block:async { ... }.Step 2: Check each option
val result = async { compute() } correctly usesasync { compute() }. Options B, C, and D misuse parentheses or operators.Final Answer:
val result = async { compute() } -> Option BQuick Check:
async uses curly braces for lambda [OK]
- Using parentheses instead of curly braces
- Trying to call async like a function with dot notation
- Using arrow syntax incorrectly
import kotlinx.coroutines.*
fun main() = runBlocking {
val deferred = async {
delay(100)
42
}
println("Result: ${deferred.await()}")
}Solution
Step 1: Understand async and await
Theasyncblock runs a coroutine that delays 100ms then returns 42. Theawait()waits for this result.Step 2: Print the awaited result
Theprintlnprints "Result: " plus the awaited value 42.Final Answer:
Result: 42 -> Option CQuick Check:
await() returns 42 after delay [OK]
- Printing Deferred object instead of awaited value
- Forgetting to call await()
- Expecting immediate result without delay
fun main() = runBlocking {
val deferred = async {
delay(100)
10 / 0
}
println(deferred.await())
}Solution
Step 1: Analyze the async block
The async block performs10 / 0, which causes a division by zero error.Step 2: Understand exception propagation
The exception is thrown whenawait()is called, as it waits for the async result.Final Answer:
Division by zero causes ArithmeticException at await() -> Option AQuick Check:
Exceptions in async appear on await() [OK]
- Thinking async can't be inside runBlocking
- Assuming delay() is invalid in async
- Ignoring exceptions thrown inside async block
fun main() = runBlocking {
val deferred1 = async { 10 }
val deferred2 = async { 20 }
val sum = ???
println(sum)
}Solution
Step 1: Understand how to get results from Deferred
Each async returns a Deferred. To get the actual value, callawait()on each.Step 2: Sum the awaited results
Usedeferred1.await() + deferred2.await()to get the sum of both results.Final Answer:
val sum = deferred1.await() + deferred2.await() -> Option DQuick Check:
Sum = await both Deferreds [OK]
- Trying to add Deferred objects directly
- Using non-existent get() method
- Calling await() as a standalone function
Ready to prove it?
Pass the test and this pattern moves unknown
