https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

Diefferson

11/28/2018, 1:12 PM
Hi, based on class Result I create class ResultAsync to encapsulate Coroutines results, what do you think about?
u

uzzu

11/28/2018, 1:50 PM
Did you try using
runCatching
? https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/run-catching.html You can use it as below:
Copy code
//In your class that extends CoroutineScope
fun getUser() {
    launch {
        runCatching { userRepository.getUser() }
            .onSuccess { /**/ }
            .onFailure { /**/ }
    }
}
d

Diefferson

11/28/2018, 3:26 PM
I know is possible with this way, but is necessary use launch block always, and I am a little lazy 😅
However if in your application you have a default error handler that return a friendly error you can implements in this method and you always will have a friendly error on onFailure
u

uzzu

11/28/2018, 4:54 PM
Yeah, I just feel you. 😄 > necessary use launch block always. I meant that probably you can use
runCatching
to replace
ResultAsync<T>
(in your gists) to
Result<T>
.
3 Views