Hide Java Futures

I have two functions, each of which returns instances CompletebleFuture<Boolean>, and I want them to be orin one ordered and short circuit.

public CompletableFuture<Boolean> doA();
public CompletableFuture<Boolean> doB();

Not future code (i.e. returning only boolean values) will simply be

return doA() || doB();

Using Futures I reached this point when the return type is an instance CompletableFuture<CompletableFuture<Boolean>>.

doA.thenApply(b -> {
  if (!b) {
    return doB();
  } else {
    return CompletableFuture.completedFuture(b);
  }
}

Is there any way to smooth this out? Or in some way can I make a return type CompletablyFuture<Boolean>?

Edit: Please note that the possibility of short-circuiting futures is the function I want. I know that I run the calculations in serial mode, but this is normal. I do not want to run doBwhen it doAreturns true.

+4
1

thenCompose thenApply:

CompletableFuture<Boolean> result = doA().thenCompose(b -> b
    ? CompletableFuture.completedFuture(Boolean.TRUE) : doB());
+2

Source: https://habr.com/ru/post/1538138/


All Articles