'Flux delayElements without initial delay

I would like to emit elements where elements are delayed each other by some time without initial delay.

I can not use

Flux.delayElements(Duration)

because it delays initial delay before first element which is not expected in my case.

Consider having example

Flux.range(1, n)

I would like to have below scenario:

1 delay 2 delay 3 delay ...

Current version of Flux.delayElements work like this:

delay 1 delay 2 delay 3 ...

My current workaround

Flux.interval(
    Duration.ZERO,
        Duration.ofSeconds(1)
        )
// make some call

but it cause problem similar to: Spring WebFlux (reactor). Error when zipWith - Could not emit tick due to lack of requests

Could you suggest some other solution for this case or maybe there should be raised issue to introduce new method in Flux to cover it?



Solution 1:[1]

You could use Mono.delay() to control delay.

@Test
void test() {
    var stream = Flux.range(1, 10)
            .concatMap(i -> 
                    Mono.delay(Duration.ofSeconds(i))
                            .thenReturn(i)
            )
            .log();

    StepVerifier.create(stream)
            .thenConsumeWhile(rec -> true)
            .verifyComplete();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex