Home > Blockchain >  Running async coroutine with Pair return
Running async coroutine with Pair return

Time:02-22

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)
}

CodePudding user response:

The problem is that you can only destructure the Pair this way, but not the Deferred itself.

You could first assign the deferred value to a single variable, and later await it so you get a Pair that you can destructure:

val deferredPair = async { twoValues(num) }

// do other stuff concurrently

val (b, c) = deferredPair.await()
println(b)
println(c)

If you don't really need to do anything concurrently with this twoValues(), you could also just call it directly (as you did in your last example):

val (b, c) = twoValues(num)
println(b)
println(c)

Note that here your suspend function does its whole computation and then returns the Pair of the 2 computed things. It doesn't offer the possibility to await parts of it. This is probably what you're looking for, so it should be OK.

If you needed to have 2 independent computations that you could await independently, you would need to launch 2 coroutines instead, which would complicate things a bit. Most likely if this is what you want, the computations are likely independent enough to be put into 2 different suspend functions which you can call in 2 separate async (so you're back to the known situation of having a single return value).

  • Related