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

Joan Colmenero

03/02/2020, 12:42 PM
Is there any way to combine 2 calls like this :
Copy code
val time = measureTimeMillis {
    val one = async { doSomethingUsefulOne() }
    val two = async { doSomethingUsefulTwo() }
    println("The answer is ${one.await() + two.await()}")
}
println("Completed in $time ms")
because if for instance one.await() crashes what will happen? or if both crashes, is there any way to handle these errors? Or in case I just want to continue with the execution instead of adding the values just printing them, is there any way to print the first one (the one that did not crash) and the others doesn't (if it crashes)?
m

miqbaldc

03/02/2020, 1:02 PM
I guess this is fit with use case functionality that #arrow has
j

Joan Colmenero

03/02/2020, 1:13 PM
In case I wouldn't use Arrow is there any way?
s

streetsofboston

03/02/2020, 1:19 PM
“Exceptional Exceptions for Coroutines made easy…?” by Anton Spaans https://link.medium.com/gKOAkJoMw4
l

louiscad

03/02/2020, 1:21 PM
Use
coroutineScope
around the two
async
calls (but nested in the
measureTimeMillis
lambda.
☝️ 2
j

Joan Colmenero

03/02/2020, 1:59 PM
keeping both async blocks?
l

louiscad

03/02/2020, 2:36 PM
If you need parallelization, yes, otherwise, the first one delays starting the second one, potentially making the whole process slower.
5 Views