When I want to make multiple async calls which may fail with fallback, I have to employ some boilerplate code.
supervisorScope {
val aAsync = async { makeRequest() }
val bAsync = async { makeAnotherRequest() }
val a = runCatching { aAsync.await() }.getOrElse { mydefault }
val b = runCatching { bAsync.await() }.getOrElse { mydefault }
}
I propose adding suspend fun <T> Deferred<T>.awaitResult(): Result<T> for more convenient usage.
val a = aAsync.awaitResult().getOrElse { mydefault }
Should be inner cancellation propagated? /shrug (for usecases when not using supervisorScope)
As a general primitive, I'm more worried about an outer cancellation. It looks like awaitResult should still throw CancellaationException when the coroutine _it is running in_ is cancelled, but should should wrap and encapsulate the cancellation of the coroutine that it is waiting for.
I sometimes use:
inline fun <T> CoroutineScope.asyncCatching(crossinline block: suspend () -> T): Deferred<Result<T>> =
async { runCatching { block() } }
@fvasco could you please check if https://github.com/Kotlin/kotlinx.coroutines/issues/2130#issuecomment-656605027 works when using the new IR compiler?
I had a very similar instance where it crashed: https://youtrack.jetbrains.com/issue/KT-43536
Most helpful comment
I sometimes use: