Member-only story
@Async — wait for completion
In the previous article, we looked at how to use the async annotation to execute code on different threads. In this article, we’ll continue with our previous example.
If we wanted to do something only after the async method has completed execution, we can make the async method return a Future. If the async method was returning something, we can just wrap the return in a Future and if it was a void method, we can return a dummy value wrapped in a Future.
This enables us to perform a check like this to know whether the async method(s) have been completed.
Example without using Future
Let’s say we want to notify our customers of their order status after we’ve performed all our business logic.
We have a method that just logs that the order is invalid.

If we run our app and send a request, we can see from the logs that our order confirmation method is the first thing to get executed.
Obviously, we don’t want this to happen, so let’s have a look at using Future to only execute this method when the other two have been completed.
Example using Future
Let’s change our two async methods to return a completable future.