Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda