title: return something as soon as a response to an async request is received using Elasticsearch's Java High Level Rest Client
body: <p>I'm using Elasticsearch's Java High Level Rest Client, I want the createProfileDocument method to return something as soon as I get a response to the Async request (as if the return statement was inside the onResponse method), I have  made this work around (code below) but I believe there is a better way to do it, which i didn't find in documentations. Here's my code:</p>
  
  <pre><code>private IndexResponse response = null;
  
  public String createProfileDocument(ProfileDocument document) throws Exception {
      UUID uuid = UUID.randomUUID();
      document.setId(uuid.toString());
      IndexRequest indexRequest = new IndexRequest("profiles", "doc", document.getId())
              .source(convertProfileDocumentToMap(document));
      ActionListener&lt;IndexResponse&gt; listener;
      listener = new ActionListener&lt;IndexResponse&gt;() {
          @Override
          public void onResponse(IndexResponse indexResponse) {
              response = indexResponse;
              //I want it to behave as if the return statement was here
          }
  
          @Override
          public void onFailure(Exception e) {
              e.printStackTrace();
          }
      };
      client.indexAsync(indexRequest, RequestOptions.DEFAULT, listener);
      //waiting for response, shouldn't be done this way
      while (response == null) {
  
      }
      IndexResponse responseClone = response;
      response = null;
      return responseClone.getResult().name().toString();
  }
  </code></pre>