Coroutines
Write asynchronous code sequentially with suspend functions.
What you will be able to do
- Use suspend for async logic.
- Use viewModelScope or lifecycleScope on Android.
- Use Dispatchers.IO for blocking IO.
- Cancel work when the screen or scope ends.
In plain English
A suspend function can pause without blocking a thread. Coroutines run in a scope with a dispatcher. Structured concurrency ties child jobs to the parent so cancellation and errors propagate predictably.
import kotlinx.coroutines.*
suspend fun loadLessons(): List<String> {
delay(100)
return listOf("HTML", "CSS")
}
fun main() = runBlocking {
val lessons = loadLessons()
println(lessons)
}Your turn
MediumLoad on IO
Switch to Dispatchers.IO inside a suspend function.
KOTLIN · starter
suspend fun load()Check your understanding before moving on.
Knowledge checkQuestion 1
What makes a function suspending?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.