Kotlin·Kotlin from syntax to a coroutine app

Coroutines

Write asynchronous code sequentially with suspend functions.

Beginner 15 min read Lesson 6 of 300 Challenge included
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.

Coroutines.ktKotlin
import kotlinx.coroutines.*

suspend fun loadLessons(): List<String> {
    delay(100)
    return listOf("HTML", "CSS")
}

fun main() = runBlocking {
    val lessons = loadLessons()
    println(lessons)
}
Your turn

Load on IO

Medium

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 in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Android with Compose
Lesson 6 of 300 · 0% of course