earlyCompleteIf

@ExperimentalCoroutinesApi
fun <T> Flow<T>.earlyCompleteIf(stopPredicate: suspend (T) -> Boolean): Flow<T>

Completes the Flow early if the specified stopPredicate is met for any element.

Return

A new Flow that completes early if the stopPredicate is met for any element.

Example usage:

flowOf(1, 2, 3, 4, 5)
.earlyCompleteIf { it == 3 }
.collect(::println) //1, 2, 3

Parameters

stopPredicate

A suspend function that takes an element of type T and returns a Boolean. If the function returns true, the flow will complete early and stop emitting elements.