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

Mark Allanson

06/18/2021, 7:51 AM
If I have a list of Deferred's created inside a
supervisorScope
using
async
. What is the most efficient way to await for the first failure of any of the jobs? At the moment I am looping the jobs, saving away successes, and capturing the first failure, then when that happens issuing a "controlled cancellation on the others" and using a yield (or delay) inside the tight loop, but it feels like there must be a better way of doing this
coroutineScope
doesn't work for me here because depending on input I need to either allow the remaining jobs to complete, or force them to fail.
e

ephemient

06/18/2021, 8:43 AM
awaitAll() will throw the first exception if any
then you can simply cancel all rest, it's a no-op if a job is already complete
m

Mark Allanson

06/18/2021, 8:46 AM
👍
Thanks!
Not sure how I didn't run into awaitAll
Yep this worked well for my use case, converted about 40 lines of code to 10
3 Views