Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda