简体   繁体   中英

How can we compose 3 or more completablefutures while maintaining all the results

I have 3 services which I want to chain

CompletableFuture<String> serviceA
CompletableFuture<String> serviceB(String resultFromA)
CompletableFuture<String> serviceC(String resultFromA, String resultFromB)

If I use thenCompose, I can't seem to maintain the first result

ie

serviceA.thenCompose(a -> serviceB(a))
        .thenCompose(b -> serviceC(a, b));  // a is lost

If I use CompletableFuture.allOf(), I don't see that it allows chaining - running in sequence and passing results.

I am going to modify serviceB, so that it returns a Pair, or some composite object, but is there a better way?

serviceA.thenCompose(a -> serviceB(a).thenCompose(b -> serviceC(a, b)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM