title: Getting data from elasticsearch into a Javascript Array()
body: <p>I'm trying to program a <code>nodejs</code> server that read all documents in an <code>elsaticsearch</code> index, and return a <code>javascript</code> array that contains the <code>title</code> field for each document.</p>
  
  <p>This is my JavaScript code:</p>
  
  <pre><code>let elasticsearch = require('elasticsearch')
  
  const esClient = new elasticsearch.Client({
      host: '127.0.0.1:9200',
      log: 'error'
  });
  
  let titles = new Array()
  
  esClient.search({
      index: 'myindex',
      type: 'mytype',
      body: {
          size: 5,
          from: 0,
          query: {
              match_all: {}
          }
      }
    }).then(function (resp) {
        var hits = resp.hits.hits;
        hits.forEach((hit, index) =&gt; titles.push(hit._source.title));
  
    }, function (err) {
        console.trace('err.message');
  });
  
  console.log(titles)
  </code></pre>
  
  <p>But the output of <code>titles</code> still empty <code>[]</code> after execution</p>
  
  <p>Do you have an idea about how should I code it? if what I want to do is possible with javascript.</p>
  
  <p>Thank you in advance</p>