I am sorry to revive this old thread, but I have the same question and it hasn’t been answered here or on other posts I could find.
If I use list.map { it.await() }
, where list
is a List<Deferred<...>>
, and any of the async tasks raise an exception, then this snippet will immediately abort because the surrounding coroutineScope
has been cancelled by the exception. It does not matter whether the exception came from the task we are currently await
ing or some other task further down the list. Assuming I don’t catch the exception inside the map { ... }
lambda, the whole waiting procedure is immediately terminated.
There could be a difference in the type of exception we get as was discussed here, but other than that I fail to see difference.
The behavior actually makes sense to me: we have suspended waiting on some result, but our scope got cancelled so the waiting is terminated. No problem there. What confuses me is the documentation of awaitAll
, posted above by @broot . Specifically the part
… which fails only when it sequentially gets to wait for the failing deferred, …
This just doesn’t seem to be true, it fails immediately, not only when “it sequentially gets to wait for the failing deferred”.
As for supervisorScope
, yes here it would make a difference because we would only get an exception once we call await
on the task that has failed. But, since we are using a supervisorScope
, awaitAll
would simply throw an exception as soon as the first exception occurs, and the supervisorScope
would wait for the other coroutines in the list to finish, so the runtime should be more or less the same in either case, whether we use awaitAll()
or map { it.await() }
(we are not cancelling anything so we have to wait for everything to finish). I can’t really think of a use-case for the combination of supervisorScope
and awaitAll
, because we would only get the first exception, and await
/awaitAll
implies we are interested in the results of each task, so we would have to use try { it.await() } catch (...) { ... }
anyway.
So, can someone explain the part of the documentation of awaitAll()
which I mentioned above? Am I missing something? Or is the wording perhaps misleading or incomplete?
Thanks in advance for any answer or thoughts on this.