suspend Function (Kotlin)
A suspend function is one marked with the suspend keyword that can pause and resume without blocking a thread. It may only be called from a coroutine or another suspend function.
Kotlin
The suspend modifier marks a function that can suspend execution at certain points (like awaiting a network call or delay), freeing the thread to do other work and resuming later where it left off. The compiler transforms it into a state machine behind the scenes.
Because suspension needs a coroutine to manage it, you can only call a suspend function from within a coroutine builder (launch, async, runBlocking) or from another suspend function. This is enforced at compile time.
Designing your async APIs as plain-looking suspend functions is idiomatic: callers write sequential code, and concurrency is opt-in via coroutine builders. suspend is what lets coroutines avoid callback hell while staying non-blocking.
Syntax
suspend fun fetchUser(id: Int): User { ... }Examples
Kotlin
import kotlinx.coroutines.*
suspend fun loadData(): String {
delay(100) // suspends, doesn't block
return "loaded"
}
fun main() = runBlocking {
println(loadData())
}Output:
loaded