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

aerb

08/05/2019, 5:29 PM
Is there a less
try
/
catch
oriented way of accessing the result of
async
? Thinking of something similar to
Result
m

Marko Mitic

08/05/2019, 5:30 PM
something like
tryAwait
?
a

aerb

08/05/2019, 5:31 PM
What is that? I can’t find any references to it
m

Marko Mitic

08/05/2019, 5:32 PM
that's hypothetical example 😄
a

aerb

08/05/2019, 5:32 PM
-_-
m

Marko Mitic

08/05/2019, 5:32 PM
I can't find functions like that either but it's quite easy to write, though
a

aerb

08/05/2019, 5:33 PM
that has been my approach - was curious if there was an endorsed way
z

Zach Klippenstein (he/him) [MOD]

08/05/2019, 6:24 PM
I would argue
deferred.runCatching { await() }
is probably more readable anyway, since it’s super obvious what it does and doesn’t require learning more API on
Deferred
.
l

louiscad

08/05/2019, 7:09 PM
An
async
will still cancel its scope though, unles its a supervisor one, or has no job (like GlobalScope)
m

Marko Mitic

08/05/2019, 7:11 PM
Yeah, I find that behavior strange
On one side, exception is lost unless you call await() but even catching it doesn't stop cancellation of parent job.
l

louiscad

08/05/2019, 7:19 PM
It's simple: either you catch it around the local
coroutineScope { … }
, or you catch it inside the
async
block. If you don't do parallelization, you most probably don't need
async
at all though, only suspending function + try catch.
1
3 Views