![](/img/trans.png)
[英]Java - CompletableFutures - How can i cancel all futures if there are exceptions
[英]How can we compose 3 or more completablefutures while maintaining all the results
我有 3 項要鏈接的服務
CompletableFuture<String> serviceA
CompletableFuture<String> serviceB(String resultFromA)
CompletableFuture<String> serviceC(String resultFromA, String resultFromB)
如果我使用 thenCompose,我似乎無法保持第一個結果
IE
serviceA.thenCompose(a -> serviceB(a))
.thenCompose(b -> serviceC(a, b)); // a is lost
如果我使用 CompletableFuture.allOf(),我看不到它允許鏈接 - 按順序運行並傳遞結果。
我要修改serviceB,讓它返回一個Pair,或者一些復合的object,但是有沒有更好的方法呢?
serviceA.thenCompose(a -> serviceB(a).thenCompose(b -> serviceC(a, b)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.