title: Spring @Async with CompletableFuture
body: <p>I have a doubt about this code:</p>
  
  <pre><code>@Async
  public CompletableFuture&lt;String&gt; doFoo() {
      CompletableFuture&lt;String&gt; fooFuture = new CompletableFuture&lt;&gt;();  
  
      try {
          String fooResult = longOp();
          fooFuture.complete(fooResult);
      } catch (Exception e) {
          fooFuture.completeExceptionally(e);
      }
  
      return fooFuture;
  }
  </code></pre>
  
  <p>The question is: does doFoo return fooFuture only after longOp has finished (either correctly or exceptionally) and is therefore returning already completed futures or is Spring doing some magic and returning before executing the body? If the code is blocking on longOp(), how would you express that the computation is being fed to an executor? </p>
  
  <p>Perhaps this? Any other way?</p>
  
  <pre><code>@Async
  public CompletableFuture&lt;String&gt; doFoo() {
  
      CompletableFuture&lt;String&gt; completableFuture = new CompletableFuture&lt;&gt;();
      CompletableFuture.runAsync(() -&gt; {
          try {
              String fooResult = longOp();
              completableFuture.complete(fooResult);
          } catch (Exception e) {
              completableFuture.completeExceptionally(e);
          }
      });
      return completableFuture;
  }
  </code></pre>