Coroutines and flows – tutorial
Learn how to use Kotlin coroutines and flows by completing this hands-on tutorial in IntelliJ IDEA.
Throughout this tutorial, you'll work on an application that loads articles and their comments from a local server. As you progress, you'll implement different loading strategies and use each one to explore a new aspect of Kotlin coroutines and flows. By the end, the application will load data concurrently, report progress, support cancellation, and handle failures while keeping the UI responsive.
To implement these loading strategies, you'll progress through the following steps:
Replace blocking calls with suspending functions
Load articles concurrently
Cancel concurrent loading with structured concurrency
Show loading progress with a flow
Emit values concurrently with
channelFlow()
Handle exceptions while loading articles
Before you start
Complete the following before starting the tutorial:
Install the latest version of IntelliJ IDEA using the JetBrains Toolbox App. Alternatively, download the standalone installer.
Make sure that JDK 17 or later is installed and configured for Gradle.
In IntelliJ IDEA, select File | New | Get from Version Control to clone the project template with this URL:
https://github.com/kotlin-hands-on/intro-coroutines-flowsCheck out the
mainbranch, which contains the unfinished tasks.
Explore the project
The desktop-client/src/jvmMain/kotlin/org/example/articles/tasks directory contains the task files you'll work with throughout the tutorial. In these files, you'll find the TODO() placeholders to complete.

If you'd like to practice writing flow tests, the desktop-client/src/jvmTest/kotlin/org/example/articles/exercises directory contains optional exercises.
You can inspect the following directories and modules to understand how the tasks connect to the application, and you'll also modify some of their files later in the tutorial:
The desktop-client module contains:
The Compose UI and view model that calls your task functions in
src/jvmMain/kotlin/org/example/articles/ui.Blocking and suspending network clients for loading articles and comments in
src/jvmMain/kotlin/org/example/articles/network.Tutorial tests referenced after each task in
src/jvmTest/kotlin/org/example/articles/tasks.Test data containing the mock service and expected results used by the tests in
src/jvmTest/kotlin/org/example/articles/data.
The project also includes:
The local Ktor server that simulates response delays and unsuccessful HTTP responses in the
servermodule.Article data and local server configuration shared by the client and server in the
sharedmodule.
You can find solutions for all the tasks on the solutions branch of the project's repository.
Run the application
Before running the application, make sure that port 9020 is available for the local server.
Select the Articles run configuration in IntelliJ IDEA and click Run
:
Alternatively, run the application from the command line with the following:
./gradlew :desktop-client:runWait for the local server to start. When it's ready, the article loader application starts in a separate window.
As you complete each task, select the corresponding option from the Loading Mode menu to see how your implementation behaves:

In the first part of the tutorial, you'll learn about blocking network calls and how to replace them with suspending functions to keep the UI responsive.