Hi there, is there any way a can rum multiple coro...
# android
v
Hi there, is there any way a can rum multiple coroutines in parallel then resume when all of then are completed ? Something like that.
Copy code
viewModelScope.launch {
    launch { a = one() }
    launch { b = two() }
    print(a+b)
}
s
Use async and then await to wait for result of both
👍 1
e
or use a scope to contain the launches
Copy code
coroutineScope {
    launch { a = one() }
    launch { b = two() }
}
print(a + b)
😟 1
although if you're not using them elsewhere, async/await could let you get rid of
var a/b
👍 1
r
val sum = async{one()}.await() + async{two()}.await()
👎 1
e
nope, order of operations there makes them run serially
Copy code
val sum = listOf(async { one() }, async { two() }).sumOf { it.await() }
if you really wanted to avoid temporary values for some reason… otherwise
Copy code
val a = async { one() }
val b = async { two() }
val sum = a.await() + b.await()
works straightforwardly
v
Awesome i will try all of it, thanks !!