0
0
Kotlinprogramming~10 mins

Async coroutine builder in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Async coroutine builder
Start main coroutine
Call async builder
Launch child coroutine
Child does work asynchronously
Child returns Deferred result
Main awaits result
Result received
Continue main coroutine
The async builder launches a child coroutine that runs concurrently and returns a Deferred result, which the main coroutine can await.
Execution Sample
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
  val deferred = async {
    delay(1000L)
    42
  }
  println("Waiting for result...")
  println("Result: ${deferred.await()}")
}
This code runs an async coroutine that waits 1 second and returns 42, which the main coroutine awaits and prints.
Execution Table
StepActionCoroutineStateOutput
1runBlocking starts main coroutinemainRunning
2async builder calledmainRunning
3async launches child coroutinechildRunning
4child coroutine delays 1000mschildSuspended
5main prints waiting messagemainRunningWaiting for result...
6main awaits deferred resultmainSuspended
7child coroutine resumes after delaychildRunning
8child coroutine returns 42childCompleted
9main resumes with result 42mainRunningResult: 42
10main coroutine completesmainCompleted
💡 Main coroutine completes after receiving and printing the async result.
Variable Tracker
VariableStartAfter Step 2After Step 8After Step 9Final
deferrednullDeferred(pending)Deferred(completed with 42)Deferred(completed with 42)Deferred(completed with 42)
Key Moments - 3 Insights
Why does the main coroutine print "Waiting for result..." before the async coroutine finishes?
Because async launches the child coroutine concurrently and immediately returns a Deferred, so the main coroutine continues running and prints before awaiting the result (see step 5 in execution_table).
What happens when main calls deferred.await()?
If the async coroutine is still running, main suspends until the result is ready. In this example, main suspends at step 6 and resumes at step 9 when the child returns 42.
Is the async coroutine blocking the main thread?
No, async runs the child coroutine concurrently without blocking. The main coroutine can do other work or suspend while waiting for the result.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the child coroutine at step 4?
ARunning
BSuspended
CCompleted
DNot started
💡 Hint
Check the 'State' column for step 4 in the execution_table.
At which step does the main coroutine resume after awaiting the async result?
AStep 5
BStep 7
CStep 9
DStep 10
💡 Hint
Look for when main prints the result after await in the execution_table.
If the delay in async was removed, how would the output change?
AMain would print "Result: 42" immediately after "Waiting for result..."
BMain would print "Waiting for result..." after the result
CMain would never print "Waiting for result..."
DMain would suspend indefinitely
💡 Hint
Without delay, async completes immediately, so await returns instantly (see variable_tracker and execution_table).
Concept Snapshot
async { ... } launches a child coroutine concurrently
Returns Deferred<T> which holds the future result
Use await() on Deferred to suspend until result is ready
Main coroutine continues running until await
Allows non-blocking asynchronous code
Full Transcript
This example shows how Kotlin's async coroutine builder works. The main coroutine starts and calls async, which launches a child coroutine that delays for 1 second and then returns 42. Meanwhile, the main coroutine prints a waiting message and then calls await on the Deferred result. If the child coroutine is still running, main suspends until the result is ready. When the child finishes, main resumes and prints the result. This allows asynchronous work without blocking the main thread.