Completed chain results

I am trying to associate method calls / results with the next call. I get a compile-time error method because if I fail to get the objB link to the previous call.

How to pass the result of the previous call to the next chain? Have I completely misunderstood this process?

Object objC = CompletableFuture.supplyAsync(() -> service.methodA(obj, width, height)) .thenApply(objA -> { try { return service.methodB(objA); } catch (Exception e) { throw new CompletionException(e); } }) .thenApply(objA -> service.methodC(objA)) .thenApply(objA -> { try { return service.methodD(objA); // this returns new objB how do I get it and pass to next chaining call } catch (Exception e) { throw new CompletionException(e); } }) .thenApply((objA, objB) -> { return service.methodE(objA, objB); // compilation error }) .get(); 
+6
source share
1 answer

You can save the intermediate CompletableFuture in a variable and then use thenCombine :

 CompletableFuture<ClassA> futureA = CompletableFuture.supplyAsync(...) .thenApply(...) .thenApply(...); CompletableFuture<ClassB> futureB = futureA.thenApply(...); CompletableFuture<ClassC> futureC = futureA.thenCombine(futureB, service::methodE); objC = futureC.join(); 
+6
source

All Articles