What is the difference between launch/join and async/await in Kotlin coroutines?
Hello! I started learning coroutines in Kotlin and got confused about two main approaches. When I write code, I see that I can start a coroutine via `launch` and then wait for its completion via `join()`. There's also `async` with `await()`. At first glance, they do the same thing — start background work and wait for the result. But I feel there is a difference, and it's important.
Here's a simple example where I'm trying to load data from two sources and combine them. With launch/join I get this:
```kotlin
fun main() = runBlocking {
var data1 = ""
var data2 = ""
val job1 = launch {
delay(1000) // simulate loading
data1 = "Data from API 1"
}
val job2 = launch {
delay(1000)
data2 = "Data from API 2"
}
job1.join()
```
No answers yet.
Log in to leave an answer