Skip to content

Instantly share code, notes, and snippets.

@skydoves
Last active February 7, 2024 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skydoves/3bb0dd7e4f5945b911248e4daa90a949 to your computer and use it in GitHub Desktop.
Save skydoves/3bb0dd7e4f5945b911248e4daa90a949 to your computer and use it in GitHub Desktop.
network_result_extensions
suspend fun <T : Any> NetworkResult<T>.onSuccess(
executable: suspend (T) -> Unit
): NetworkResult<T> = apply {
if (this is NetworkResult.Success<T>) {
executable(data)
}
}
suspend fun <T : Any> NetworkResult<T>.onError(
executable: suspend (code: Int, message: String?) -> Unit
): NetworkResult<T> = apply {
if (this is NetworkResult.Error<T>) {
executable(code, message)
}
}
suspend fun <T : Any> NetworkResult<T>.onException(
executable: suspend (e: Throwable) -> Unit
): NetworkResult<T> = apply {
if (this is NetworkResult.Exception<T>) {
executable(e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment