Sobes.tech
Junior — Middle

How can a string value from an asynchronous CompletableFuture operation be passed to the sendEmail method?

sobes.tech AI

Answer from AI

To pass a string value from an asynchronous CompletableFuture operation to the sendEmail method, you can use the thenAccept or thenApply methods to handle the result after the operation completes. For example:

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    // asynchronous operation returning a string
    return "Hello, World!";
});

future.thenAccept(result -> sendEmail(result));

// or, if sendEmail returns a CompletableFuture:
// future.thenCompose(result -> sendEmail(result));

// The sendEmail method
void sendEmail(String message) {
    // email sending logic
}

Thus, when future completes, the result will be passed to sendEmail."}]}]}