I am not sure if there is more concise way of doin...
# coroutines
d
I am not sure if there is more concise way of doing that, maybe without need of wrapping failures into something else, something that uses coroutine exceptions handlers or
select
statement, etc
h
Does your Either have the concept of a recover? Acts on Failure to return a success? I did something like that for cache lookup / db lookup on cache miss.
d
nah, it’s just a sealed class with minimal functionality
is it a standard functionality of Either to recover?
h
I wrote one for com.github.kittinunf.result.Result using method extensions:
Copy code
suspend fun <V : Any, E : Exception> SuspendableResult<V, E>.recover(transform: suspend (E) -> V): SuspendableResult<V, E> {
    return when (this) {
        is SuspendableResult.Success -> this
        is SuspendableResult.Failure -> SuspendableResult.of{ transform(this.error) }
    }
}
d
Copy code
launch (NonCancellable) {
    val first = async { ... }
    val second = async { ... }
    val results = try {
        first.await()
    } catch (e: Exception) {
        second.await()
    }
}
d
@Dico it doesn't work because exception propagates to parent
unless I use SupervisorJob when calling async
d
It propagates despite NonCancellable?
d
yeah, I don't think NonCancellable has much to do with any exceptions except CancellationException
h
Do you really want to do async though? The second task may perform useless work.
d
yeah that's fine