简体   繁体   中英

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?

您可以简单地执行未来,然后执行thenApplyAsync并返回用户,以便您返回用户的未来,并放弃计费,如下所示:

Futures.transformAsync(createUser(), user -> createBillingAccount(user).thenApplyAsync(b -> user))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM