Is there a less `try` / `catch` oriented way of ac...
# coroutines
a
Is there a less
try
/
catch
oriented way of accessing the result of
async
? Thinking of something similar to
Result
m
something like
tryAwait
?
a
What is that? I can’t find any references to it
m
that's hypothetical example 😄
a
-_-
m
I can't find functions like that either but it's quite easy to write, though
a
that has been my approach - was curious if there was an endorsed way
z
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
An
async
will still cancel its scope though, unles its a supervisor one, or has no job (like GlobalScope)
m
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
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