📖 Scenario: You are building a simple Kotlin program that fetches data from two sources asynchronously. You want to see how coroutines help make this easy and clear.
🎯 Goal: Learn how to use Kotlin coroutines to run two tasks at the same time and wait for both to finish before continuing.
📋 What You'll Learn
Create two suspend functions that simulate fetching data with delay
Use a coroutine scope to launch both tasks asynchronously
Wait for both tasks to complete and combine their results
Print the combined result
💡 Why This Matters
🌍 Real World
Coroutines help apps fetch data from the internet or do background work without freezing the screen.
💼 Career
Many Kotlin developers use coroutines to write clean, efficient asynchronous code in Android apps and backend services.
Progress0 / 4 steps
1
Create suspend functions to simulate data fetching
Write two suspend functions called fetchData1 and fetchData2. Each should delay for 1000 milliseconds and then return a string: "Data1" and "Data2" respectively.
Kotlin
Hint
Use suspend fun to create functions that can pause. Use delay(1000L) to wait 1 second.
2
Set up main function with coroutine scope
Create a main function and inside it, use runBlocking to start a coroutine scope.
Kotlin
Hint
Use fun main() = runBlocking { } to start your coroutine scope.
3
Launch both fetches asynchronously and wait for results
Inside runBlocking, use async to start fetchData1() and fetchData2() concurrently. Store their results in result1 and result2 by calling await() on each.
Kotlin
Hint
Use async { } to start tasks and await() to get their results.
4
Print the combined results
Print the combined string of result1 and result2 separated by a space using println.
Kotlin
Hint
Use println("$result1 $result2") to show the combined results.
Practice
(1/5)
1. Why are coroutines important for asynchronous programming in Kotlin?
easy
A. They allow tasks to run without blocking the main thread.
B. They make the code run slower but safer.
C. They replace all functions with loops.
D. They force the program to wait for each task to finish.
Solution
Step 1: Understand asynchronous programming
Asynchronous programming means running tasks without stopping the main program flow.
Step 2: Role of coroutines in Kotlin
Coroutines let tasks run in the background without blocking the main thread, improving responsiveness.
Final Answer:
They allow tasks to run without blocking the main thread. -> Option A
Quick Check:
Coroutines = non-blocking tasks [OK]
Hint: Coroutines keep the app responsive by not blocking main thread [OK]
Common Mistakes:
Thinking coroutines slow down the program
Confusing coroutines with loops
Believing coroutines force waiting
2. Which of the following is the correct way to start a coroutine in Kotlin?
easy
A. startCoroutine { println("Hello") }
B. run { println("Hello") }
C. launch { println("Hello") }
D. beginCoroutine { println("Hello") }
Solution
Step 1: Recall coroutine builders in Kotlin
The common builder to start a coroutine is launch.
Step 2: Check syntax correctness
launch { ... } is valid, while others like startCoroutine or beginCoroutine are not standard coroutine starters.
Final Answer:
launch { println("Hello") } -> Option C
Quick Check:
Use launch to start coroutine [OK]
Hint: Remember: launch is the coroutine starter function [OK]
Common Mistakes:
Using non-existent coroutine functions
Confusing run with launch
Trying to start coroutine without a builder
3. What will be the output of this Kotlin code using coroutines?