Can you tell me how I can wait for the CompletableFuture list in the parent CompletableFuture, collect their result and return it?
CompletableFuture<TerminateEnvironmentResponse> future = CompletableFuture.supplyAsync(() -> {
TerminateEnvironmentResponse terminateEnvironmentResponse = new TerminateEnvironmentResponse();
List<EnvironmentDto> environmentDtoList = ...
environmentDtoList.forEach(environmentDto -> {
_cloudFormationService.destroyStack(environmentDto)
.whenComplete((result, ex) -> {
if (ex != null) {
terminateEnvironmentResponse.setError(true);
} else {
int count = terminateEnvironmentResponse.getCount();
terminateEnvironmentResponse.setCount(++count);
}
});
});
return terminateEnvironmentResponse;
});
return future;