title: How to wrap around a sequence of non-blocking operation using reactive mono?
body: <p>I have a set of non-blocking operations which can start an operation, check if it's done and gets the result if it's done. The problem is that the <code>start</code> and <code>getResult</code> won't happen immediately together, but I need to return the result without blocking. Sample code below which demonstrates what I am trying to do. </p>
  
  <pre class="lang-java prettyprint-override"><code>@GetMapping("/some-mapping")
  public Mono&lt;String&gt; someops(@RequestParam(value="param") String someParam) {
      int jobId = nonBlockingOps.start(someParam); // 1. start the op
      return Mono.defer( () -&gt; {
          // wait for op to complete
          while (!nonBlockingOps.isDone(jobId)) { /// 2. check if the op finished
              try {
                  Thread.sleep(1000); // This will block
              } catch (InterruptedException e) {
                  return Mono.error(e);
              }
          }
          return Mono.just(nonBlockingOps.getResult(jobId)); // 3. return the result
      } );
  }
  </code></pre>
  
  <p>So, even though the mono is <code>defer</code>-ed, it might block when the subscription happens.</p>
  
  <p><strong>1. So how do I wrap this around with <code>Mono</code> so that mono takes care of the waiting part?</strong></p>
  
  <p>Alternatively (or additionally), you can consider three services as follows:</p>
  
  <pre class="lang-java prettyprint-override"><code>@PostMapping("/job")
  public Mono&lt;Integer&gt; opsStart(@RequestParam(value="param") String param) 
  { return Mono.just(nonBlockingOps.start(param)); }
  
  @GetMapping("/job/isDone/{id}")
  public Mono&lt;Boolean&gt; opsCheck(@PathVariable Integer id) 
  { return Mono.just(nonBlockingOps.isDone(id)); }
  
  @GetMapping("/job/getResult/{id}")
  public Mono&lt;String&gt; opsGet(@PathVariable Integer id) 
  { return Mono.just(nonBlockingOps.getResult(id)); }
  </code></pre>
  
  <p><strong>2a. So how do I achieve the same outcome by calling these functions?</strong></p>
  
  <p><strong>2b. So how do I achieve the same outcome by calling these services via <code>WebClient</code>?</strong></p>