title: Wait for Multiple Spring WebClient Mono Responses
body: <p>I am trying to call external service in a micro-service application to get all responses in parallel and combine them before starting the other computation. I know i can use block() call on each Mono object but that will defeat the purpose of using reactive api. is it possible to fire up all requests in parallel and combine them at one point. </p>
  
  <p>Sample code is as below. In this case "Done" prints before actual response comes up. I also know that subscribe call is non blocking. </p>
  
  <p>I want "Done" to be printed after all responses has been collected, so need some kind of blocking. however do not want to block each and every request</p>
  
  <pre><code>final List&lt;Mono&lt;String&gt;&gt; responseOne = new ArrayList&lt;&gt;();
      IntStream.range(0, 10).forEach(i -&gt; {
          Mono&lt;String&gt; responseMono =
                  WebClient.create("https://jsonplaceholder.typicode.com/posts")
                          .post()
                          .retrieve()
                          .bodyToMono(String.class)
                  ;
          System.out.println("create mono response lazy initialization");
          responseOne.add(responseMono);
      });
  
      Flux.merge(responseOne).collectList().subscribe( res -&gt; {
  
          System.out.println(res);
      });
      System.out.println("Done");
  </code></pre>
  
  <p>Based on the suggestion, I came up with this which seems to work for me.</p>
  
  <pre><code>StopWatch watch = new StopWatch();
      watch.start();
      final List&lt;Mono&lt;String&gt;&gt; responseOne = new ArrayList&lt;&gt;();
      IntStream.range(0, 10).forEach(i -&gt; {
          Mono&lt;String&gt; responseMono =
                  WebClient.create("https://jsonplaceholder.typicode.com/posts")
                          .post()
                          .retrieve()
                          .bodyToMono(String.class);
          System.out.println("create mono response lazy initialization");
          responseOne.add(responseMono);
      });
      CompletableFuture&lt;List&lt;String&gt;&gt; futureCount = new CompletableFuture&lt;&gt;();
      List&lt;String&gt;  res = new ArrayList&lt;&gt;();
      Mono.zip(responseOne, Arrays::asList)
              .flatMapIterable(objects -&gt; objects) // make flux of objects
              .doOnComplete(() -&gt; {
                  futureCount.complete(res);
              }) // will be printed on completion of the flux created above
              .subscribe(responseString -&gt; {
                          res.add((String) responseString);
                      }
              );
  
      watch.stop();
      List&lt;String&gt; response = futureCount.get();
      System.out.println(response);
      // do rest of the computation
      System.out.println(watch.getLastTaskTimeMillis());
  </code></pre>