Search code examples
project-reactorreactor

Executing list of Mono after completing a first Mono


Executing list of mono after completing 1 mono.

Mono<Project> mono1 = ...;

List<Mono<?>> publishers = new ArrayList<>();
 publishers.add(mono2);
 publishers.add(mono3);
 publishers.add(mono4);
            
if ( publishers.size() > 0 )
   Mono.zip(publishers, res->prj.id);

Here, I want to execute list of Mono after completing Mono1.

I can do chaining. But, Is there anyway to do with Mono.zip ...?


Solution

  • mono1.then(Mono.zip(publishers, res->prj.id));
    

    See its JavaDocs:

    /**
     * Let this {@link Mono} complete then play another Mono.
     * <p>
     * In other words ignore element from this {@link Mono} and transform its completion signal into the
     * emission and completion signal of a provided {@code Mono<V>}. Error signal is
     * replayed in the resulting {@code Mono<V>}.
     *
     * <p>
     * <img class="marble" src="doc-files/marbles/thenWithMonoForMono.svg" alt="">
     *
     * <p><strong>Discard Support:</strong> This operator discards the element from the source.
     *
     * @param other a {@link Mono} to emit from after termination
     * @param <V> the element type of the supplied Mono
     *
     * @return a new {@link Mono} that emits from the supplied {@link Mono}
     */
    public final <V> Mono<V> then(Mono<V> other) {