Skip to content

Instantly share code, notes, and snippets.

@eymar
Last active July 5, 2018 15:17
Show Gist options
  • Save eymar/0bcaa8410f6e3dc3daf983845cf19be1 to your computer and use it in GitHub Desktop.
Save eymar/0bcaa8410f6e3dc3daf983845cf19be1 to your computer and use it in GitHub Desktop.
suspend fun <T> Deferred<Response<T>>.handleResponse(): Deferred<Response<T>> {
val it = await()
throw HttpCodeException(500)
return this
}
suspend fun <T> Deferred<T>.awaitHandlingError(
success: (T) -> Unit, error: (t: Throwable) -> Unit) {
try {
success(this.await())
} catch (t: Throwable) {
error(t)
}
}
fun <T> asyncNetworkCall(networkState: NetworkState,
block: suspend CoroutineScope.() -> T): Deferred<T> {
if (!networkState.isConnected()) {
throw NoConnectionException()
}
return async(block = block)
}
suspend fun <T> Deferred<T>.awaitAndApply(action: (T) -> Unit) : T {
val result = this.await()
action(result)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment