Created
September 17, 2023 17:03
-
-
Save khahani/f197df48e2d9403c121f72bc5c6d5c56 to your computer and use it in GitHub Desktop.
asResult.kt
This file contains hidden or 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
| import kotlinx.coroutines.flow.Flow | |
| import kotlinx.coroutines.flow.catch | |
| import kotlinx.coroutines.flow.map | |
| import kotlinx.coroutines.flow.onStart | |
| fun <T> Flow<T>.asResult(): Flow<Result<T>> { | |
| return this | |
| .map<T, Result<T>> { | |
| Result.Success(it) | |
| } | |
| .onStart { emit(Result.Loading) } | |
| .catch { emit(Result.Error(it)) } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment