Last active
July 5, 2018 15:17
-
-
Save eymar/0bcaa8410f6e3dc3daf983845cf19be1 to your computer and use it in GitHub Desktop.
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
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