Skip to content

Unexpected Behavior When Using .take(1) in Reactor Flux #34638

@lfygh

Description

@lfygh

I encountered an unexpected behavior when using .take(1) in my Reactor Flux pipeline. Below is the code snippet I used:

Flux.just("", "b")
    .doOnEach(signal -> {
        System.out.println("signal " + signal);
    })
    .filter(StringUtils::isNotBlank)
    .map(e -> {
        System.out.println("map element " + e);
        return e;
    })
    .subscribe();

Expected Behavior

Without .take(1), the output is as follows:

signal doOnEach_onNext()
signal doOnEach_onNext(b)
map element b
signal onComplete()

This behavior aligns with my expectations.

Unexpected Behavior

However, when I add .take(1), like this:

Flux.just("", "b")
    .doOnEach(signal -> {
        System.out.println("signal " + signal);
    })
    .filter(StringUtils::isNotBlank)
    .map(e -> {
        System.out.println("map element " + e);
        return e;
    })
    .take(1)
    .subscribe();

The output changes to:

map element b
signal doOnEach_onNext(b)

This is different from the previous case, and I have two questions:

  1. Why does adding .take(1) change the behavior?
  2. Why does map element b appear before signal doOnEach_onNext(b) in the output?

I would appreciate any insights or explanations regarding this behavior. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions