title: Using CompletionStage to return results
body: <p>I am new to play framework and Java8. I am trying to write a simple web service in Play which conforms to the Asynch model and allows the web service call to return quickly, leaving heavy lifting for async thread.</p>
  
  <p>I have a very basic operation </p>
  
  <pre><code>public CompletionStage&lt;Result&gt; getPeople() {
  
  }
  </code></pre>
  
  <p>The real processing in the call has to load the persons data from database. The db method is </p>
  
  <pre><code>List&lt;People&gt; people = dbService.getPeople();
  return ok(Json.tojson(people));
  </code></pre>
  
  <p>The dbMethod is itself async and returns CompletionStage>.</p>
  
  <pre><code>CompletionStage&lt;List&lt;People&gt;&gt; getPeople() {}
  </code></pre>
  
  <p>So my problem is that I fail to understand how best to program web service to that the method does not block.</p>
  
  <p>I can do something like this in my handler</p>
  
  <pre><code>public CompletionStage&lt;Result&gt; getPeople() {
    CompletableFuture.completedFuture(
      ok(Json.toJson(dbservice.getPeople().get())));
  }
  </code></pre>
  
  <p>Problem with this is that the controllel blocks at "get" method and also that it throws exception which I don't know how to handle other than using standard try-catch block (which also makes it non-async).</p>
  
  <p>I have also tried something like this</p>
  
  <pre><code>return CompletableFuture.supplyAsync(() -&gt; {
      return dbService.getPeople();
      }).thenApply(i -&gt; { ok(Json.toJson(i.get())); } );
  </code></pre>
  
  <p>But that gives compile errors.</p>
  
  <p>I will really appreciate some help here; as I am very new to Java async programming and despite reading lots of tutorials I am unclear how to best use these.</p>
  
  <p>Thank you all.</p>