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

Dariusz Kuc

08/16/2019, 8:16 PM
hello, was wondering if there is some nice idiomatic way of error handling for awaiting for a list of deferred to complete, e.g.
awaitAll
will throw first encountered exception Is there a better way than
Copy code
val results: List<Deferred<Any>> = ... // whatever
results.joinAll()
for (result in results) {
  val error = result.getCompletionExceptionOrNull()
  if (error != null) {
    // handle error
  } else {
    result.await()
  }
}
l

louiscad

08/16/2019, 8:38 PM
Either you handle errors outside of the local
coroutineScope { … }
inside which you start your `async`/`launch` child coroutines, or you handle errors inside the `async`/`launch` blocks themselves, recovering and letting other coroutines of the scope continue to execute.
d

Dariusz Kuc

08/16/2019, 9:03 PM
👍 doing the
try/catch
within
async
block should do the trick, then i can just do
awaitAll()