Fred Friis
05/26/2023, 4:46 PMFrancesc
05/26/2023, 4:50 PMsupervisorScope
Fred Friis
05/26/2023, 4:51 PMFred Friis
05/26/2023, 5:44 PMresult1 = doThis() //Result
result2 = doThis() //Result
result3 = doThis() //Result
//do something with the results
i just want to do the same except
1) simultaneously
2) without short circuiting all three on one failure
3) without messing with the results (ie by faking a successful result with onrecover etc)jw
05/26/2023, 5:46 PMFred Friis
05/26/2023, 5:47 PMjw
05/26/2023, 5:49 PMjw
05/26/2023, 5:49 PMFred Friis
05/26/2023, 5:50 PMjw
05/26/2023, 6:00 PMJob
is returned from a launch { }
. A Deferred
is returned from an async { }
.jw
05/26/2023, 6:01 PMFred Friis
05/26/2023, 6:01 PMDjuro
05/26/2023, 9:27 PMjoinAll
and awaitAll
would have no effect if you launch them as child coroutines as in that case coroutine which is supposed to defer the result will just propagate it up the job hierarchy. If no handler is installed in top-level coroutines or scope it will propagate it upwards to the thread’s uncaught exception handler.
This is the reason why supervisorScope
was recommended to you in the first place. This way parent job is SupervisorJob
making your coroutines launched with async
now top-level coroutines which behaves differently and won’t now propagate the exception meaning you would be able to get the result of coroutines that did not fail.louiscad
05/28/2023, 10:36 PMrunCatching
inside your async blocks, and then use awaitAll
on it and decide what to do.Francesc
05/29/2023, 12:23 AMrunCatching
is tricky to use with coroutines as you could easily swallow the cancellation exceptionlouiscad
05/29/2023, 8:25 AMcoroutineScope { … }
that contains the async { … }
blocks, so it should be fine. You can also make a cancellable()
extension for Result<T>
that rethrows `CancellationException`s.Francesc
05/29/2023, 3:01 PMlouiscad
05/29/2023, 11:38 PMFred Friis
05/30/2023, 6:32 PMsuspend fun doStuff(){
result1 = doThis() //Result
result2 = doThis() //Result
result3 = doThis() //Result
//do something with the results
}
Result here means runCatching { ... } yielding a Result on all three
I know runCatching is unidiomatic, especially inside coroutines, but that's the standard in these codebases and I'm unable to change that at the moment
I don't think awaitAll would do what I'm asking, since the docs literally state awaitAll fails fast ie on the first error in any of the three, that's not what I want, I want them all to finish even if one or they all fail, no different to as if they were sequential code (like above), only that instead of sequantial run, they run simultaneouslyFred Friis
05/30/2023, 6:34 PMlouiscad
05/30/2023, 6:39 PMcoroutineScope { }
Fred Friis
05/30/2023, 6:40 PM