0
0
Kotlinprogramming~10 mins

Suspend functions concept in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Suspend functions concept
Call suspend function
Suspend execution
Other work runs
Resume suspend function
Complete function and return
Shows how a suspend function pauses its work, lets other code run, then resumes later.
Execution Sample
Kotlin
import kotlinx.coroutines.*

suspend fun fetchData(): String {
    delay(1000)
    return "Data"
}

fun main() = runBlocking {
    val result = fetchData()
    println(result)
}
This code calls a suspend function that waits 1 second, then returns a string.
Execution Table
StepActionStateOutput
1Call fetchData()Start fetchData, delay begins
2delay(1000) suspends fetchDatafetchData paused, main continues
3runBlocking waits for fetchDataWaiting for fetchData to resume
4After 1 second, fetchData resumesfetchData resumes after delay
5fetchData returns "Data"fetchData completes
6main receives resultresult = "Data"
7println(result)Prints to consoleData
8Program endsAll done
💡 fetchData completes and returns, main prints result, program ends
Variable Tracker
VariableStartAfter Step 4After Step 6Final
resultundefinedundefined"Data""Data"
Key Moments - 3 Insights
Why does fetchData pause at delay(1000) instead of blocking?
Because delay is a suspend function that pauses fetchData without blocking the thread, letting other code run (see Step 2 in execution_table).
How does main wait for fetchData to finish?
main uses runBlocking which waits for fetchData to complete before continuing (see Step 3 and Step 6).
What happens after fetchData resumes from suspension?
It continues running from where it paused, returns the result, and main receives it (see Steps 4 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does fetchData pause execution?
AStep 4
BStep 2
CStep 6
DStep 7
💡 Hint
Check the 'Action' and 'State' columns in Step 2 where delay suspends fetchData.
According to variable_tracker, what is the value of 'result' after Step 6?
A"Data"
Bnull
Cundefined
Dempty string
💡 Hint
Look at the 'After Step 6' column for 'result' in variable_tracker.
If delay was replaced with a blocking Thread.sleep, how would the execution_table change?
Amain would continue without waiting
BfetchData would suspend as before
CfetchData would not pause; main would block immediately
DProgram would crash
💡 Hint
Consider how blocking calls differ from suspend functions in Kotlin coroutines.
Concept Snapshot
suspend fun name() { ... } defines a suspend function.
Inside, suspend functions like delay() pause without blocking.
Caller must be in coroutine or runBlocking to call suspend functions.
Execution suspends and resumes later, enabling non-blocking waits.
Useful for asynchronous tasks like network or delay.
Full Transcript
This visual trace shows how Kotlin suspend functions work. When fetchData is called, it starts and hits delay(1000), which suspends fetchData without blocking the thread. The main function waits using runBlocking. After 1 second, fetchData resumes, returns "Data", and main prints it. Variables like result change from undefined to "Data" after fetchData completes. Key points: suspend functions pause and resume, delay suspends without blocking, and runBlocking waits for suspend functions to finish.