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

203
Views
Las corrutinas de Kotlin obtienen resultados desde su lanzamiento

Soy nuevo en kotlin y su concepto coroutine.

Tengo debajo de la rutina usando withTimeoutOrNull -

 import kotlinx.coroutines.* fun main() = runBlocking { val result = withTimeoutOrNull(1300L) { repeat(1) { i -> println("I'm with id $i sleeping for 500 ms ...") delay(500L) } "Done" // will get cancelled before it produces this result } println("Result is $result") }

Producción -

 I'm sleeping 0 ... Result is Done

Tengo otro programa coroutine sin tiempo de espera -

 import kotlinx.coroutines.* fun main() = runBlocking { val result = launch { repeat(1) { i -> println("I'm sleeping $i ...") delay(500L) } "Done" // will get cancelled before it produces this result } result.join() println("result of coroutine is ${result}") }

producción -

 I'm sleeping 0 ... result of coroutine is StandaloneCoroutine{Completed}@61e717c2

¿Cómo puedo obtener el resultado del cálculo en kotlin coroutine cuando no uso withTimeoutOrNull como mi segundo programa?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

launch no devuelve nada, por lo que debe:

  1. Use async y await (en cuyo caso, await devuelve el valor)

     import kotlinx.coroutines.* fun main() = runBlocking { val asyncResult = async { repeat(1) { i -> println("I'm sleeping $i ...") delay(500L) } "Done" // will get cancelled before it produces this result } val result = asyncResult.await() println("result of coroutine is ${result}") }
  2. No use el lanzamiento en absoluto ni mueva su código que está dentro del lanzamiento a una función de suspensión y use el resultado de esa función:

     import kotlinx.coroutines.* fun main() = runBlocking { val result = done() println("result of coroutine is ${result}") } suspend fun done(): String { repeat(1) { i -> println("I'm sleeping $i ...") delay(500L) } return "Done" // will get cancelled before it produces this result }
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!