https://kotlinlang.org logo
#coroutines
Title
# coroutines
f

Fernando Branbila Cunha Junior

09/10/2020, 1:05 PM
How can i properly pass the value of a async function so i can use "outside" it? I need to do 1 for each block like:
val bar = async { foo() }
Or there's a way to do like:
async { val bar = foo() }
and then use bar value?
g

gildor

09/10/2020, 1:47 PM
What do you try to achieve? Do you have some context/use case
It looks that you don't need async in this case
f

Fernando Branbila Cunha Junior

09/10/2020, 2:20 PM
I'm trying to learn co-routines. Saw some tutorials but i'm still kind lost. What i'm trying to archive is to use N suspended functions and to retrive the value of them and use on a non-suspended function. Like:
Copy code
fun main(){
  val val1 = async { func1() }
  val val2 = async { func2(val1.await()) }
  println(val2)
}
g

gildor

09/10/2020, 2:30 PM
Just drop all async, you don't need them Async is required only to do work in parallel/concurrently, but nothing is parallel in this example, all operations are sequential
I recommend to read official guide, especially this part https://kotlinlang.org/docs/reference/coroutines/composing-suspending-functions.html
f

Fernando Branbila Cunha Junior

09/10/2020, 2:43 PM
i undertand that, in this example, i don't get any paralellism and it isn't a great applicability, but it's an example of what i'm trying to do to understand more, but i'm kind lost, mainly on how to use values generateded by async { } calls
s

Saurabh

09/10/2020, 2:59 PM
You can do something like this
val deferred = async { foo() }
val bar = deferred.await()
f

Fernando Branbila Cunha Junior

09/10/2020, 3:07 PM
hmm, will try to. Thanks. I've been strugling on doing something like a CRUD, specifically on a "return a list of objects" using coroutines
u

uli

09/10/2020, 3:24 PM
you need a coroutine. try `runBlocking()`:
Copy code
fun main(){
  runBlocking { 
    val val1 = func1()
    val val2 = func2(val1)
    println(val2)
  }
}
In the special case of
main
you can also declare main as suspending:
Copy code
suspend fun main(){
  val val1 = func1()
  val val2 = func2(val1)
  println(val2)
}
g

gildor

09/10/2020, 3:31 PM
https://kotlinlang.slack.com/archives/C1CFAFJSK/p1599749942211100?thread_ts=1599743109.208100&cid=C1CFAFJSK It will not work. Use runBlocking as Uli suggested, it's a way to connect standard functions and suspend (asynchronous) functions
I think you confusion comes from wrong approach to switching between blocking and asynchronous code as you doing it in your simple example 3 times, you should try to use different approach, you launch coroutine (with runBlocking, launch or suspend main) and do all work there, and switch back to blocking code (and use runBlocking) only in special cases (like block program while it executing), switching multiple times between blocking and asynchronous code (not only with coroutines) is usually not the best strategy, it requires more blocked threads and just not very efficient
s

Saurabh

09/10/2020, 3:38 PM
My bad. Didn't see the replies. Thought the OP is already inside a coroutine scope. 😅
g

gildor

09/10/2020, 3:39 PM
If it already in coroutine scope, then there is no reason to use async{} and next line call await on it, it's the same as just remove async completely and just inline code from async lambda
s

Saurabh

09/10/2020, 3:42 PM
Of course you wouldn't do async await like this. But you have to look at the fact that the question is not about practical implications of coroutines. I thought the question was simpler and about trying to return a value out of an async block.
g

gildor

09/10/2020, 3:43 PM
I've been strugling on doing something like a CRUD, specifically on a "return a list of objects" using coroutines
What kind issue do you have exactly? You can return list of objects with coroutines, or you want to get each item from this list asynchronously? If so, see awaitAll() function and extension with the same name, idea is create multiple Deferred (using async{}) and await all of them together, as result you will get a list of resulting values
6 Views