flatMapRight

fun <L, R, T> <Error class: unknown class><Either<L, R>>.flatMapRight(block: suspend (R) -> <Error class: unknown class><T>): <Error class: unknown class><Either<L, T>>

flatMapLatest the Flow> into Flow>.

Parameters

block

function to run on Either.Right value and that returns Flow

Usecase for it: we have 2 functions, one of it returns Flow, and the second - just Flow and is depends on the Right value from the first and we need to combine Right value from the first and the result from the second, or emit 1 error (Either.Left) if it occurs. Use that fun to have better code style.

Example1:

fun getUserId(): Either fun getFriendsFroUser(userId: ID): Flow>

data class MeWithFriends(val myId: ID, val friends: List)

val observeMeWithFriends: Flow> = getUserId().flatMapRight { id -> getFriendsFroUser(id).map { MeWithFriends(id, it) }}

Example2:

fun getUserId(): Either fun getMyFriends(): Flow>

data class MeWithFriends(val myId: ID, val friends: List)

val observeMeWithFriends: Flow> = getUserId().flatMapRight { id -> getMyFriends().map { MeWithFriends(id, it) }}