Skip to content

Instantly share code, notes, and snippets.

@khahani
Created September 17, 2023 17:03
Show Gist options
  • Select an option

  • Save khahani/f197df48e2d9403c121f72bc5c6d5c56 to your computer and use it in GitHub Desktop.

Select an option

Save khahani/f197df48e2d9403c121f72bc5c6d5c56 to your computer and use it in GitHub Desktop.
asResult.kt
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