I'm able to call a function using async when its returning just one value.
However, if the return is a Pair, I get - Destructuring declaration initializer of type Deferred<Unit> must have a 'component1()' function
Am I missing something?
Here is a sample code:
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val num = 11
val a = async { oneValue(num) }
println(a.await()) // returns 22
// This doesn't compile
val (b, c) = async { twoValues(num) } // Destructuring declaration initializer of type Deferred<Unit> must have a 'component1()' function
println(b.await())
println(c.await())
// Same code works without async-await
val (d, e) = twoValues(num)
println(d)
println(e)
}
suspend fun oneValue(num: Int): Int {
return num * 2
}
suspend fun twoValues(num: Int): Pair<Int, Boolean> {
return Pair(num * 2, true)
}