# How to wait for asynch requests to finish

**URL:** <https://discuss.elastic.co/t/how-to-wait-for-asynch-requests-to-finish/136569>\
**Category:** Elasticsearch\
**Created:** [June 19, 2018, 8:31pm UTC](https://discuss.elastic.co/t/how-to-wait-for-asynch-requests-to-finish/136569 "2018-06-19T20:31:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman\_G](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@Roman\_G](https://discuss.elastic.co/u/Roman_G)\
**Post date:** [June 19, 2018, 8:31pm UTC](https://discuss.elastic.co/t/how-to-wait-for-asynch-requests-to-finish/136569/1 "2018-06-19T20:31:20Z")

</div>

I am using bulk request to insert/update data in Elastic, I send a bunch of asynch requests with 10k documents in each one.

What I want to do is at the end of the process to wait for each request to finish to make sure all the docs came through.

What would be the best way to do this? Is there something I can call to see if there is something still pending?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 19, 2018, 8:39pm UTC](https://discuss.elastic.co/t/how-to-wait-for-asynch-requests-to-finish/136569/2 "2018-06-19T20:39:47Z")

</div>

> I send a bunch of asynch requests with 10k documents in each one.

How do you send these requests? Why not just wait for the response from these request?

---

<div class="post-metadata">

**Author:** ![Roman\_G](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@Roman\_G](https://discuss.elastic.co/u/Roman_G)\
**Post date:** [June 20, 2018, 3:38pm UTC](https://discuss.elastic.co/t/how-to-wait-for-asynch-requests-to-finish/136569/3 "2018-06-20T15:38:14Z")

</div>

I assume that it's faster not to wait and send asynch and let the server process requests.

I can probably wait, but why have asynch then? There should be a way to check if the things finished processing after all the requests are submitted, otherwise you don't even know what happened to them.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 20, 2018, 4:23pm UTC](https://discuss.elastic.co/t/how-to-wait-for-asynch-requests-to-finish/136569/4 "2018-06-20T16:23:07Z")

</div>

I am not quite sure what you mean by async. Could you explain?

---

<div class="post-metadata">

**Author:** ![Roman\_G](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@Roman\_G](https://discuss.elastic.co/u/Roman_G)\
**Post date:** [June 20, 2018, 4:54pm UTC](https://discuss.elastic.co/t/how-to-wait-for-asynch-requests-to-finish/136569/5 "2018-06-20T16:54:55Z")

</div>

We send the request with data and as one of the parameters we give a callback class. The request returns almost immediately and when it succeeds or fails the callback is called.

Actually I think I found my answer here:  
[https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-low-usage-requests.html#\_multiple\_parallel\_asynchronous\_actions](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-low-usage-requests.html#_multiple_parallel_asynchronous_actions)

it uses CountDownLatch

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 20, 2018, 7:42pm UTC](https://discuss.elastic.co/t/how-to-wait-for-asynch-requests-to-finish/136569/6 "2018-06-20T19:42:25Z")

</div>

Makes sense now. So you can use the same pattern with countdown latch. Just check the resUlt to make sure no errors occurred. When count down latch goes to 0 and you don't have no errors that means all your documents are indexed on primary and all currently available replicas.

---

<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:** [July 18, 2018, 7:42pm UTC](https://discuss.elastic.co/t/how-to-wait-for-asynch-requests-to-finish/136569/7 "2018-07-18T19:42:38Z")

</div>

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