因此,考虑到我有以下示例:
CompletionStage<String> tokenFuture = getToken();
CompletionStage<CompletionStage<CompletionStage<CompletionStage<Boolean>>>> result = tokenFuture.thenApply(token -> {
WSRequest request = ws.url(url).setHeader("Authorization", "Bearer " + token);
CompletionStage<WSResponse> response = request.post(json);
return response.thenApply(r -> {
if (r.getStatus() == 201) {
return CompletableFuture.supplyAsync(() -> CompletableFuture.supplyAsync(() -> true));
} else {
return getToken().thenApply(t -> {
WSRequest req = ws.url(url).setHeader("Authorization", "Bearer " + t);
return req.post(json).thenApply(b -> b.getStatus() == 201);
});
}
});
});
我的问题是整个CompletionStage<CompletionStage<CompletionStage<CompletionStage<Boolean>>>>
多嵌套的未来类型。是否可以CompletionStage<Boolean>
通过使用 Scala 之类的东西来减少它,flatMap
还是有另一种方法可以做到这一点?