简体   繁体   中英

Getting the result of a CompletableFuture.supplyAsync

I have this piece of code:

CompletableFuture
    .supplyAsync(() -> {
        return  smsService.sendSMS(number);
    }).thenApply(result -> {
        LOG.info("SMS sended " + result);
    });

but I got a compilation error:

The method thenApply(Function<? super Boolean,? extends U>) in the type CompletableFuture<Boolean> is not applicable for the arguments ((<no type> result) -> {})

You want to use thenAccept not thenApply

thenApply takes a Function which is of the form

public interface Function<T, R> {
    R apply(T t);
}

thenAccept takes a Consumer which is of the form

public interface Consumer<T> {
    void accept(T t);
}

The lambda you provided does not have a return value; it is void. Because a generic type parameter cannot be void your lambda cannot be casted as a Function interface. On the other hand, the Consumer has a void return type which the lambda could satisfy.

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