# Await is a reserved word - Using ES API to load list of hits by index

**URL:** <https://discuss.elastic.co/t/await-is-a-reserved-word-using-es-api-to-load-list-of-hits-by-index/142740>\
**Category:** Kibana\
**Created:** [August 2, 2018, 10:06am UTC](https://discuss.elastic.co/t/await-is-a-reserved-word-using-es-api-to-load-list-of-hits-by-index/142740 "2018-08-02T10:06:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asallemi](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@asallemi](https://discuss.elastic.co/u/asallemi)\
**Post date:** [August 2, 2018, 10:06am UTC](https://discuss.elastic.co/t/await-is-a-reserved-word-using-es-api-to-load-list-of-hits-by-index/142740/1 "2018-08-02T10:06:34Z")

</div>

Hello,

I am using Kibana to load list of hits by index name using ElasticSearch API.  
The best way to do that, is using **[Scroll API](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-scroll)** and this is my **routes.js** code:

```
const elasticsearch = require('elasticsearch');

export default function (server) {

  const config = server.config();
  const client = new elasticsearch.Client({ host: config.get('elasticsearch.url') });
  
  server.route({
    path: '/api/indices_view/index/{name}',
    method: 'GET',
    handler(req, reply) {

      const allHits = [];
      const responseQueue = [];

      // start things off by searching, setting a scroll timeout, and pushing
      // our first response into the queue to be processed
      client.search({
        index: 'shakespeare',
        scroll: '30s', // keep the search results "scrollable" for 30 seconds
        q: 'type:act'
      })

      while (responseQueue.length) {
        const response = responseQueue.shift();

        // collect the titles from this response
        response.hits.hits.forEach(function (hit) {
          allHits .push(hit.fields.title);
        });

        // check to see if we have collected all of the titles
        if (response.hits.total === allHits .length) {
          console.log('every "test" title', allHits );
          break
        }

        // get the next response if there are more titles to fetch
        responseQueue.push(
          client.scroll({
            scrollId: response._scroll_id,
            scroll: '30s'
          })
        );
      }
    }
  });
}

```

I know.. the example uses Await operator. If I use it, I got this error in my console **await is a reserved word** , else I got nothing.

I'll appreciate any help 😃  
Best regards.

---

<div class="post-metadata">

**Author:** ![Stacey\_Gammon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stacey_gammon/32/14025_2.png) [@Stacey\_Gammon](https://discuss.elastic.co/u/Stacey_Gammon)\
**Post date:** [August 2, 2018, 7:07pm UTC](https://discuss.elastic.co/t/await-is-a-reserved-word-using-es-api-to-load-list-of-hits-by-index/142740/2 "2018-08-02T19:07:21Z")

</div>

it's because the handler function isn't marked as async. change to:

`async handler(req, reply) {`

---

<div class="post-metadata">

**Author:** ![asallemi](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@asallemi](https://discuss.elastic.co/u/asallemi)\
**Post date:** [August 3, 2018, 7:03am UTC](https://discuss.elastic.co/t/await-is-a-reserved-word-using-es-api-to-load-list-of-hits-by-index/142740/3 "2018-08-03T07:03:12Z")

</div>

Thanks for your reply @Stacey_Gammon 😃

I added **async** before the handler function, but I got this error:

> error [06:47:16.302] [warning][process] [illegal\_state\_exception] source and source\_content\_type parameters are required :: {"path":"/shakespeare/\_search","qu  
> ery":{"scroll":"30s","source":["type"],"q":"type:act"},"statusCode":500,"response":"{"error":{"root\_cause":[{"type":"illegal\_state\_exception","reason"  
> :"source and source\_content\_type parameters are required"}],"type":"illegal\_state\_exception","reason":"source and source\_content\_type parameters are re  
> quired"},"status":500}"}

Is this an issue too ? ☹

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 31, 2018, 7:03am UTC](https://discuss.elastic.co/t/await-is-a-reserved-word-using-es-api-to-load-list-of-hits-by-index/142740/4 "2018-08-31T07:03:14Z")

</div>

Closed on August 31, 2018, 7:03am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
