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

Kroppeb

11/03/2019, 2:09 AM
I want to select between 2
Deferred<V>
but only fail if both fail. Would this work/is there a better way to do it?
d

Dico

11/03/2019, 8:51 AM
What do you mean by fail? Cancellation or thrown error?
For the latter, you should make sure to start them in a
supervisorScope {}
k

Kroppeb

11/03/2019, 11:43 AM
They are completabledeferred, when I asked the question I meant completedexceptionally
o

octylFractal

11/03/2019, 7:19 PM
personally I would do:
Copy code
select<Int> {
                t.onJoin {
                    try {
                        t.await()
                    } catch (e: Exception) {
                        o.await()
                    }
                }
                o.onJoin {
                    try {
                        o.await()
                    } catch (e: Exception) {
                        t.await()
                    }
                }
            }
with the try-catch probably pulled out to a function
👍 1
d

Dico

11/03/2019, 7:21 PM
I'd make sure to add some logic for suppressed exceptions as well.
o

octylFractal

11/03/2019, 7:22 PM
yes, probably another try-catch around the second
await
. more reasons to make it its own function 🙂
d

Dico

11/03/2019, 7:23 PM
Or just some logic with
if (isCompletedExceptionally)
3 Views