Home > Software engineering >  Flux takeUntil only takes one element
Flux takeUntil only takes one element

Time:09-02

Can someone help me understand what is going on here with Flux's takeUntil operator?

Flux.just(1, 2, 3, 4, 5)
    .takeUntil { it < 4 }
    .map { println("Flux:$it") }
    .subscribe()

In the console, the only thing that is printed is:

Flux:1

I expected to see

Flux:1
Flux:2
Flux:3

Why do I only see one element?

CodePudding user response:

Please, note that you are using the takeUntil() operator:

Relay values from this Flux until the given Predicate matches. This includes the matching data (unlike takeWhile(java.util.function.Predicate<? super T>)).

Flux (reactor-core 3.4.22).

Please, note: «until»:

until the given Predicate matches

To achieve the desired behavior, please, consider using the takeWhile() operator instead:

Relay values from this Flux while a predicate returns TRUE for the values (checked before each value is delivered). This only includes the matching data (unlike takeUntil(java.util.function.Predicate<? super T>)).

Flux (reactor-core 3.4.22).

  • Related