title: CompletableFuture | thenApplyAsync vs thenCompose and their use cases
body: <p>I was trying to understand CompletableFuture, and came across 2 methods, thenApplyAsync and thenCompose.
  Am trying to understand the difference between these two.</p>
  
  <pre><code>CompletableFuture&lt;String&gt; completableFuture = CompletableFuture.supplyAsync(() -&gt; {
      System.out.println(Thread.currentThread().getName() + " Printing hello");
      return "Hello";
  }).thenCompose((String s) -&gt; {
    return CompletableFuture.supplyAsync(() -&gt; {
        System.out.println(Thread.currentThread().getName() + " Adding abc");
        return "abc "+s;});
  }).thenApplyAsync((String s) -&gt; {
      System.out.println(Thread.currentThread().getName() + " Adding world");
      return s + " World";
  }).thenApplyAsync((String s) -&gt; {
      System.out.println(Thread.currentThread().getName() + " Adding name");
      if (false) {
         throw new RuntimeException("Oh no exception");
      }
      return s + " player!";
  }).handle((String s, Throwable t) -&gt; {
      System.out.println(s != null ? s : "BLANK");
      System.out.println(t != null ? t.getMessage() : "BLANK Exception");
      return s != null ? s : t.getMessage();
  });
  </code></pre>
  
  <p>How I interpreted these methods as, <code>thenApplyAsync</code> will execute the supplied function in a different thread and return a result, but internally it is wrapped inside a CompletionStage. Whereas, <code>thenCompose</code> will get the return a reference to a CompletionStage.</p>
  
  <p>So under what scenarios, will I use <code>thenCompose</code> over <code>thenApplyAsync</code>?</p>
  
  <p>Went through this link, but it was talking about <code>thenApply</code>, which is somewhat different:
  <a href="https://stackoverflow.com/questions/43019126/completablefuture-thenapply-vs-thencompose">CompletableFuture | thenApply vs thenCompose</a></p>