Kotlin Help

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:

First step Replace blocking calls with suspending functions
Second step Load articles concurrently
Third step Cancel concurrent loading with structured concurrency
Fourth step Show loading progress with a flow
Fifth step Emit values concurrently with channelFlow()
Sixth step Handle exceptions while loading articles

Before you start

Complete the following before starting the tutorial:

  1. Install the latest version of IntelliJ IDEA using the JetBrains Toolbox App. Alternatively, download the standalone installer.

  2. Make sure that JDK 17 or later is installed and configured for Gradle.

  3. 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-flows
  4. Check out the main branch, 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.

The coroutines and flows tutorial project structure

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 server module.

  • Article data and local server configuration shared by the client and server in the shared module.

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.

  1. Select the Articles run configuration in IntelliJ IDEA and click Run Run icon:

    Select the articles run configuration, and click run

    Alternatively, run the application from the command line with the following:

    ./gradlew :desktop-client:run
  2. Wait 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:

The Articles application with the Loading Mode menu

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.

Next step

01 December 2026