kane
kane

Reputation: 6027

What is proper way to chain a future without transforming it?

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?

Upvotes: 1

Views: 444

Answers (1)

Pablo Matias Gomez
Pablo Matias Gomez

Reputation: 6823

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))

Upvotes: 2

Related Questions