Skip to content

Instantly share code, notes, and snippets.

View jacobras's full-sized avatar

Jacob Ras jacobras

View GitHub Profile
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())
class MyUseCase {
suspend operator fun invoke() = runCatching { delay(1000) }
}
scope.launch {
myUseCase()
.onSuccess { println("Success") }
.onFailure { println("Failure") }
println("We're not stopping!")
}