This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.CancellationException | |
/** | |
* Like [runCatching], but with proper coroutines cancellation handling. Also only catches [Exception] instead of [Throwable]. | |
* | |
* Cancellation exceptions need to be rethrown. See https://github.com/Kotlin/kotlinx.coroutines/issues/1814. | |
*/ | |
inline fun <R> resultOf(block: () -> R): Result<R> { | |
return try { | |
Result.success(block()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyUseCase { | |
suspend operator fun invoke() = runCatching { delay(1000) } | |
} | |
scope.launch { | |
myUseCase() | |
.onSuccess { println("Success") } | |
.onFailure { println("Failure") } | |
println("We're not stopping!") | |
} |