Pihentagy
getLineById(...) = runBlocking { Deferred<SomeType> someDeferred = async { dependant.thatCanThrow() } SomeType? res = someDeferred.awaitOrNull(); } suspend inline fun <T> Deferred<T>.awaitOrNull(): T? = runCatching { this.await() }.getOrNull()
awaitOrNull
@MockkBean private lateinit var dependent: Dependent @SpykBean private lateinit var controller: MainController every { dependent.thatCanThrow() } throws Exception("omg") controller.getLineById(...)
every { dependent.thatCanThrow() } throws Exception("omg")
Sam
async
runBlocking
await
getLineById
Deferred<SomeType?>
Deferred<SomeType> someDeferred = async { dependant.thatCanThrow() }
Deferred<SomeType> someDeferred = async { try { dependant.thatCanThrow() } catch(e: Exception) { null } }
asyncOrNull
public fun <T> CoroutineScope.asyncOrNull( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T ): Deferred<T?> = async(context, start) { try { block() } catch (e: Exception) { null } }
A modern programming language that makes developers happier.