-1

Consider below code as I am not able to find better words to ask the question:

CompletionStage<Manager> callingAsyncFunction(int developerId) {
    return getManagerIdByDeveloperId(developerId)
           .thenCompose(id ->
             getManagerById(id, mandatoryComputationToGetManager(id)))
}

mandatoryComputationToGetManager() returns a CompletionStage

Now the doubt which I have is :

I wanted to call mandatoryComputationToGetManager() and after its computation I want getManagerById(...) to be called.

I know there can be one way i.e. calling thenCompose() first to do mandatoryComputationToGetManager() and then do another thenCompose() on previous result for getManagerById(). But I wanted to figure out if there is a way without piping one thenCompose() o/p to another by which I can hold until mandatoryComputationToGetManager() result is ready.

As far as I understand, in the above code getManagerById() will get called even if the result is not yet ready from mandatoryComputationToGetManager(), which I want to wait for so that once mandatoryComputationToGetManager() give the result getManagerById() should get computed asynchronously.

Yash Khare
  • 355
  • 2
  • 10
  • 1
    Doesn't `mandatoryComputationToGetManager()` require any parameter? Otherwise you could call it outside and then combine the results before calling `getmanagerById()`. Also, what are the parameters for the latter? Does it expect a `CompletionStage` or some result type? In the first case you would have to handle it inside that method, in the second case nested `thenCompose()` is the way to go. – Didier L Jul 02 '18 at 15:02
  • You should try to make your question as complete as possible with the minimal information required, and describe what you have tried – see also [mcve]. – Didier L Jul 05 '18 at 07:57
  • Thats a good catch , mandatoryComputationToGetManager() - requires a param, I should have framed the question more clearly. And mandatoryComputationToGetManager() would expect a normal result type which would be id which got computed already by getManagerIdByDeveloperId. Nested thenCompose in the way which could be used, Thanks Didier. – Yash Khare Jul 05 '18 at 07:59
  • Best is that you edit your question to include all the necessary info :-) – Didier L Jul 05 '18 at 08:02

1 Answers1

2

Ideally, we should pipe one thenCompose o/p to another, but there is a way by which we can achieve what you are trying to do.

  CompletionStage<String> callingAsyncFunction(int developerId) {
    return getManagerIdByDeveloperId(developerId)
        .thenCompose(id -> getManagerById(id, mandatoryComputationToGetManager()));
  }

  private CompletionStage<String> getManagerById(
      Integer id, CompletionStage<String> stringCompletionStage) {
    return stringCompletionStage.thenApply(__ -> "test");
  }

  private CompletionStage<String> mandatoryComputationToGetManager() {
    return CompletableFuture.completedFuture("test");
  }
Rahil
  • 139
  • 9
  • Thanks Rahil. Thats a pretty good idea of putting a thenApply for stringCompletionStage inside getManagerById and then I can go for further computations which I want. – Yash Khare Jul 05 '18 at 08:11