Fernando Branbila Cunha Junior
09/10/2020, 1:05 PMval bar = async { foo() }
Or there's a way to do like:
async { val bar = foo() }
and then use bar value?gildor
09/10/2020, 1:47 PMFernando Branbila Cunha Junior
09/10/2020, 2:20 PMfun main(){
val val1 = async { func1() }
val val2 = async { func2(val1.await()) }
println(val2)
}
gildor
09/10/2020, 2:30 PMFernando Branbila Cunha Junior
09/10/2020, 2:43 PMSaurabh
09/10/2020, 2:59 PMval deferred = async { foo() }
val bar = deferred.await()
Fernando Branbila Cunha Junior
09/10/2020, 3:07 PMuli
09/10/2020, 3:24 PMfun main(){
runBlocking {
val val1 = func1()
val val2 = func2(val1)
println(val2)
}
}
In the special case of main
you can also declare main as suspending:
suspend fun main(){
val val1 = func1()
val val2 = func2(val1)
println(val2)
}
gildor
09/10/2020, 3:31 PMSaurabh
09/10/2020, 3:38 PMgildor
09/10/2020, 3:39 PMSaurabh
09/10/2020, 3:42 PMgildor
09/10/2020, 3:43 PMI've been strugling on doing something like a CRUD, specifically on a "return a list of objects" using coroutinesWhat 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