I'm doing some code that involves a number of parallel requests in batchs from elements in a list and I'm facing a strange behaviour:
import kotlinx.coroutines.*
suspend fun main() {
coroutineScope {
val hello = listOf("hello", "world")
val chunks = hello.chunked(1) {
async { translate(it.first()) }
}
chunks.forEach {
print(it.await())
}
}
}
private fun translate(word: String): String {
return if(word == "hello") {
"hola"
} else {
"mundo"
}
}
It should display "holamundo" but sometimes this example prints "mundomundo" instead.
I made it available on Kotlin Playground as well.
What I'm missing here?