-
Notifications
You must be signed in to change notification settings - Fork 44
Open
Description
I have an existing async called wrapped inside a Try and working with Futures.sequence
e.g.
List<CompletableFuture<Try<Foo>>> requests = [...]
CompletableFuture<List<Try<Baz>>> items = Futures.sequence(requests);
List<Try<Baz>> result = items.join()
.stream()
.map(try -> myFunction(try))
.collect(Collectors.toList())
...
private Try<Baz> myFunction(Try<Foo> fooTry) {
return fooTry.flatMap(foo -> Try.ofFailable(() -> process(foo)));
}
is there an example/support for two async calls? such as
List<CompletableFuture<Try<Foo>>> requests1 = [...]
List<CompletableFuture<Try<Bar>>> requests2 = [...]
// requests1.size(), requests2.size() are the same value
List<CompletableFuture<Try<Baz>>> results = new ArrayList<>();
for (int i = 0; i < items1.size(); i++) {
CompletableFuture<Try<Baz>> combined = items1.get(i).thenCombine(items2.get(i), (fooTry, barTry) -> myFunction(fooTry, barTry));
results.add(combined);
}
List<Try<Baz>> results = results.stream().map(CompletableFuture::join).collect(Collectors.toList());
...
private Try<Baz> myFunction(Try<Foo> fooTry, Try<Bar> barTry) {
return fooTry.flatMap(foo -> barTry.flatMap(bar -> Try.ofFailable(() -> process(foo, bar))));
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels