zokipirlo
02/23/2022, 9:52 AMsupervisorScope{
list.map {
async { doSomeApiCall(it) }
}.awaitAll()
}
2.
supervisorScope {
list.map {
async { doSomeApiCall(it) }
}
}.awaitAll()
ephemient
02/23/2022, 9:55 AM.awaitAll()
isn't needed either way because a scope does not end until all its children are completedJoffrey
02/23/2022, 9:56 AMawaitAll
still converts Deferred
into their inner value, and I guess the code is incomplete here but I'm assuming the OP would want to get those values. However I agree that it doesn't await anything anymore, they will all be immediately converted. If the result of awaitAll()
is really ignored like in the exemple, it's indeed pointless.supervisorScope
after map
. In the first case, the extra code would be executed after the map, while in the second case it will run concurrently with the map. But there is no such code in your examplemap
. It's less visible when applied to supervisorScope
. Also I like to see supervisorScope
as blocks, so calling a function on it seems weird.Sam
02/23/2022, 10:06 AMsupervisorScope
returns anyway. If you were to await them outside the scope, you're really just unwrapping them after they're already finished. Awaiting them inside the scope is more representative of what will actually happen.zokipirlo
02/23/2022, 10:21 AMawaitAll
is needed because it starts async
block and without awaitAll
nothing will execute.
All examples I saw was with async
block, that's why I did it like that. I guess I could change async
to launch
because result from suspend function is ignored in my case?Joffrey
02/23/2022, 10:23 AMawaitAll()
. Also, launch
is better because it expresses that you don't care about the resultSam
02/23/2022, 10:35 AMasync
is to start eagerly, just the same as launch
(you can change that by passing a CouroutineStart
argument). But there are some circumstances where that still might not start immediately, for example when using runBlocking
, so maybe that accounts for the examples that you saw.bezrukov
02/23/2022, 8:57 PMephemient
02/23/2022, 9:10 PMsupervisorScope
, other async calls may fail without notice. which is somewhat similar but not the same as the behavior ofcoroutineScope {
list.forEach {
launch { doSomeApiCall(it) }
}
}
bezrukov
02/23/2022, 9:45 PMIn list order
It is not true, awaitAll fails once the first in execution order fails, from the doc:
This function is not equivalent to deferreds.map { it.await() } which fails only when it sequentially gets to wait for the failing deferred, while this awaitAll fails immediately as soon as any of the deferreds fail.
ephemient
02/23/2022, 9:51 PMzokipirlo
02/23/2022, 10:10 PMephemient
02/23/2022, 10:19 PM.join()
the jobsgildor
02/24/2022, 5:44 AMSo the safest solution is with launch because I don’t want others be cancelled if any exception happens?If this is your explicit requirement, I wouldn’t bother with supervisorScope at all and instead wrap doSomeApiCall to try/catch and required error handling