Assuming I'm not using globals Scopes or superviso...
# coroutines
j
Assuming I'm not using globals Scopes or supervisor scopes, is there any difference between launch and an async that returns a deferred<unit>?
k
launch returns a job, not a deferred.
I suppose
Deferred
is a job, though, so 🤷
c
I think the errors within the async coroutine are thrown at the point where
deferred.await()
is called, rather than the
async { }
function call itself.
j
Doesn't structured concurrency make the errors from the async cancel the parent without calling await?
c
The result of the deferred is available when it is completed and can be retrieved by await method, which throws an exception if the deferred had failed.
It should still cancel the parent, but the exception thrown from the coroutine should be captured within the deferred and only thrown through
deferred.await()
. This is in contrast to
launch
which directly sends the captured exception to the
CoroutineExceptionHandler
j
in this example from the docs https://pl.kotl.in/_apz1GtoX two cancels everything without ever actually being awaited on https://kotlinlang.org/docs/composing-suspending-functions.html#structured-concurrency-with-async
My suspicion here is that launch and async used to be a lot more distinguishable before structured concurrency was introduced, but now the differences are only notable if you somehow escape structured concurrency (not counting the difference in the return type)
p
I feel like there’s no difference between async and launch w.r.t parent cancellation Only difference is async returns a value on .await() and that value can actually be a thrown exception if it completed exceptionally