Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Running async coroutine with Pair return

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!