CompletableFuture<NodeRenderer> (List<CompletableFuture<Boolean>> futures, int index, List<NodeRenderer> renderers) { if (index >= futures.size()) { return CompletableFuture.completedFuture(null); } return futures.get(index).thenCompose(res -> { if (res) { return CompletableFuture.completedFuture(renderers.get(index)); } else { return getFirstApplicableRenderer(futures, index + 1, renderers); } }); }