Coroutine Basics in Kotlin
From the Kotlin cheat sheet · Coroutines · verified Jul 2026
Coroutine Basics
Launching and managing coroutines
kotlin
// Coroutine scope and launch
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
delay(1000)
println("World")
}
println("Hello")
}
// Async and await
suspend fun fetchUser(): User {
delay(1000) // Simulated network call
return User("Alice")
}
suspend fun fetchPosts(): List<Post> {
delay(1000)
return listOf(Post("Title"))
}
fun main() = runBlocking {
val user = async { fetchUser() }
val posts = async { fetchPosts() }
println("User: ${user.await()}")
println("Posts: ${posts.await()}")
}
// Suspend functions
suspend fun doWork() {
delay(1000)
println("Work done")
}💡 suspend functions can call other suspend functions
⚡ async for concurrent execution, await for results
📌 Structured concurrency ensures child coroutines complete
🟢 Use runBlocking only in main or tests