title: How to access previous stage results in CompletableFuture
body: <p>I am trying to chain tasks in CompletableFuture to execute them in parallel and get the best performance and resource utilization.</p>
  <p>The workflow I am writing has five stages and each stage depends on the result of the previous stages.</p>
  <p>The workflow execution is as follows:</p>
  <pre><code>* Stage-1: Create CustomerContext -&gt; This will call downstream services and return Context object.
      
  * Stage-2: Generate Candidates for the customer -&gt; This stage generate candidates and returns a List&lt;Candidate&gt; object. This stage requires CustomerContext as a parameter to generate the candidates. 
      
  * Stage-3: Process Candidate -&gt; In this stage I process the candidates and returns processed List&lt;Candidate&gt;. This stage requires CustomerContext and List&lt;Candidate&gt; object from stage-1 and stage-2 
      
  * Stage-4: Rank candidates -&gt; In this stage I perform candidate ranking and returns ranked List&lt;Candidate&gt; object. This stage requires CustomerContext and List&lt;Candidate&gt; object from stage-1 and **stage-3** 
        
  * Stage-5: Generate Result -&gt; In this stage I generate the final result and returns Result object by making downstream API calls., This stage requires CustomerContext and List&lt;Candidate&gt; object from **stage-1** and **stage-4** 
  </code></pre>
  <p>I can create a result holder object to hold each stage's results. However, I am not sure if that is the best solution.</p>
  <p>Is CompletableFuture the best solution for this use case? What would be the optimal way to chain these stages?</p>