louiscad
10/24/2018, 2:22 PMlaunch {
val someWorkAsync = async {
throw CustomException()
}
delay(100)
try {
someWorkAsync.await()
} catch (e: CustomException) {
println("Handled it! $e")
}
}
If I understood correctly, now, the whole scope is cancelled, even though the exception is handled properly when calling await()
. How can I get the previous behavior? Create a Job
that has scope's job as parent, and use it for async(…) { … }
?marstran
10/24/2018, 2:24 PMsupervisorScope { }
for that.Vsevolod Tolstopyatov [JB]
10/24/2018, 2:26 PMasync(SupervisorJob(coroutineContext[Job]))
😞Vsevolod Tolstopyatov [JB]
10/24/2018, 2:27 PMwithContext
should be used instead of single async
.
And for multiple asyncs there is coroutineScope
Vsevolod Tolstopyatov [JB]
10/24/2018, 2:30 PMelizarov
10/24/2018, 2:53 PMelizarov
10/24/2018, 3:00 PMCancellationException
, so you will not be able to catch exception with await
. Thanks @[removed] for pointing that out.louiscad
10/24/2018, 10:56 PMDeferred
outside the coroutine, so you can await it somewhere else. I thought I used it for single line requestPermission(…)
call on Android, but it turns out I use CompletableDeferred
instead, so I think I have no use case at the moment.