Retry logic with CompletableFuture : <p>I need to submit a task in an async framework I'm working on, but I need to catch for exceptions, and retry the same task multiple times before "aborting".</p>

<p>The code I'm working with is:</p>

<pre><code>int retries = 0;
public CompletableFuture&lt;Result&gt; executeActionAsync() {

    // Execute the action async and get the future
    CompletableFuture&lt;Result&gt; f = executeMycustomActionHere();

    // If the future completes with exception:
    f.exceptionally(ex -&gt; {
        retries++; // Increment the retry count
        if (retries &lt; MAX_RETRIES)
            return executeActionAsync();  // &lt;--- Submit one more time

        // Abort with a null value
        return null;
    });

    // Return the future    
    return f;
}
</code></pre>

<p>This currently doesn't compile because the return type of the lambda is wrong: it expects a <code>Result</code>, but the <code>executeActionAsync</code> returns a <code>CompletableFuture&lt;Result&gt;</code>. </p>

<p>How can I implement this fully async retry logic?</p>
