title: Using Mono methods with dynamic parameters
body: <p>I have a function that return the input as a Mono:</p>
  
  <pre><code>public static Mono&lt;Integer&gt; emitter(int param){
      return Mono.just(param)
              .delayElement(Duration.ofMillis(100)); //delay to simulate http response
  }
  </code></pre>
  
  <p>I'd like to call the emitter once with an initial value of 3 but then have it repeat until certain size is reached. This repeat logic should be in the main method so I can't modify the <code>emitter()</code>.</p>
  
  <pre><code>public static void main(String[] args){
      int maxSize = 5;
      int initial = 3;
      Mono&lt;Integer&gt; response = emitter(initial);
  
      response
          .doOnNext(s -&gt; {
              System.out.println("need more!");
          })
          .subscribe();
  }
  </code></pre>
  
  <p>One naive solution would be:</p>
  
  <pre><code>public static void main(String[] args){
      int maxSize = 5;
      int initial = 3;
  
      for(int i = 0; i &lt; 999; i++) {
          Mono&lt;Integer&gt; response = emitter(initial+i);
          Mono&lt;Boolean&gt; isDone = response
                  .flatMap(elem -&gt; {
                      if(elem &lt; maxSize) {
                          System.out.println("need more!");
                          return Mono.just(false);
                      } else {
                          System.out.println("ok done!");
                          return Mono.just(true);
                      }
                  });
          if(isDone.block())
              break;
      }
  }
  </code></pre>
  
  <p>Basically, I'm trying to create another Mono with dynamic parameters based on the result of the previous Mono. I know that Mono/Flux are immutable...
  Is there a neat and reactive way of doing this?
  I've tried things like <code>Flux.range(0, Integer.MAX_VALUE).zipWith(myMono)</code> to try and feed parameters into the emitter but couldn't make it work.</p>
  
  <p>PS. I know my example doesn't make much sense. I've tried to simplify my real world scenario that involves lists and spring WebFlux(emitter).</p>
  
  <p>Thanks!</p>
  
  <p>---EDIT</p>
  
  <p>Ok, here's what I came up with:</p>
  
  <pre><code>public static void main(String[] args) throws InterruptedException {
      int maxSize = 5;
      int initial = 3;
  
      Flux.range(initial, 10)
              .delayElements(Duration.ofSeconds(1))  
              .flatMap(param -&gt; emitter(param))
              .flatMap(it -&gt; {
                  if(it &lt; maxSize) {
                      System.out.println("need more!: " + it);
                      return Mono.just(false);
                  } else {
                      System.out.println("done!: " + it);
                      return Mono.just(true);
                  }
               })
              .takeUntil(Boolean::booleanValue)
              .subscribe();
  
      Thread.sleep(6000);
  }
  </code></pre>
  
  <blockquote>
  <pre><code>need more!: 3
  need more!: 4
  done!: 5
  </code></pre>
  </blockquote>
  
  <p>One problem is that if I don't delay <code>Flux.range</code>, the execution is not done in order and it's possible for more or less print statements to be output than the 3 lines expected.</p>