There is a very intuitive operator named delayElements
available in the @ProjectReactor publisher Flux to introduce a delay between each element emitted. Say, for example, following cod emit an element per second.
Flux.fromIterable(List.of(1, 2, 3))
.delayElements(Duration.ofSeconds(1))
.map(i -> i * 2)
.doOnNext(r -> System.out.println(ZonedDateTime.now().toEpochSecond() + ": " + r))
.blockLast();
To get the same behavior done in @Rxjava, I had to do some ceremony.
final List<Integer> data = List.of(1, 2, 3);
Flowable.interval(1, TimeUnit.SECONDS)
.take(data.size())
.map(i -> data.get(i.intValue()) * 2)
.doOnNext(r -> System.out.println(ZonedDateTime.now().toEpochSecond() + ": " + r))
.blockingLast();
Does anyone know a better way to do the above in @Rxjava?
I found a rather simple approach here. Thanks, Mina.
Flowable.just(1,2,3)
.zipWith(Flowable.interval(1, TimeUnit.SECONDS), (item, interval) -> item)
.subscribe(r -> System.out.println(r));