简体   繁体   中英

CompletionStage, CompletableFuture Void - what to return?

Here is my method:

public CompletionStage<Void> insert(List<HashAction> hashActionList) {
    if(!hashActionList.isEmpty()) {
        return ...
    }
    // what to return here ?
}

I have no idea of what to return if my list is empty. Not sure that null is good since I would have to check for null afterwards.

I tried

return CompletableFuture.completedFuture(null);

But I'm not really convinced since I chose randomly one implementation of CompletionStage

You can just return CompletableFuture<Void> by just having empty async run method

public CompletionStage<Void> insert(List<String> hashActionList) {
    if(!hashActionList.isEmpty()) {
        return null;
    }
    return CompletableFuture.runAsync(()->{});
}

OR you can use thenAccept to return CompletionStage<Void> and avoid null

return CompletableFuture.completedFuture(null).thenAccept(i->{});

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