Suppose I have 2 functions that return a type of Future.
ListenableFuture<User> createUser();
CompletableFuture<BillingAccount> createBillingAccount(User user);
I need to run createUser()
before running createBillingAccount
because it depends on the user created.
However, I want to return the user, not the billingAccount.
Normally, I would use Futures.transformAsync(createUser(), user -> createBillingAccount(user))
but this would return a BillingAccount. I want to return a user.
What else can I do here without blocking?
CodePudding user response:
You can simply execute the future and then do thenApplyAsync
and return the user, so that you are returning a future of User, and discard the billing, like this:
Futures.transformAsync(createUser(), user -> createBillingAccount(user).thenApplyAsync(b -> user))