Java CompletableFuture: Avoid callback hell
<p>I have a question regarding CompletableFuture in Java. I'm waiting for a CompletableFuture to complete and depending on the received result, I want to either call a new task and wait for the CompletableFuture to complete or do something different. I'm not happy with my solution since there are so many callbacks and it is different to read. Could you help my to improve my code?</p>

<pre><code>final CompletableFuture&lt;String&gt; future = new CompletableFuture&lt;&gt;();

final ActorRef processCheckActor = actorSystem.actorOf(
    springExtension.props("ProcessCheckActor"), "processCheckActor-" + new Random().nextInt());

final CompletableFuture&lt;Object&gt; checkResponse =
    PatternsCS.ask(processCheckActor, new ProcessToCheckMessage(processId), TIMEOUT)
        .toCompletableFuture();

checkResponse.thenAccept(obj -&gt; {
  final ProcessCheckResponseMessage msg = (ProcessCheckResponseMessage) obj;
  if (msg.isCorrect()) {
    final CompletableFuture&lt;Object&gt; response =
        PatternsCS.ask(processSupervisorActor, new ProcessStartMessage(processId), TIMEOUT)
            .toCompletableFuture();

    response.thenAccept(obj2 -&gt; {
      future.complete("yes");
    });
  } else {
    future.complete("no");
  }
});
</code></pre>