Xinchao
Xinchao

Reputation: 3543

How to complete a CompletableFuture<Void>?

I want a CompletableFuture that only signals the completion (e.g. I don't have a return value).

I can instantiate the CompletableFuture as:

CompletableFuture<Void> future = new CompletableFuture<> ();

But what should I feed into the complete method? For example, I can't do

future.complete(new Void());

Upvotes: 29

Views: 18840

Answers (2)

Tom Carmi
Tom Carmi

Reputation: 594

A bit shorter (and more elegant) way to write it would be:

return CompletableFuture.completedFuture(null);

Upvotes: 3

Mureinik
Mureinik

Reputation: 311228

As you've noticed, you cannot instantiate a Void object like this. Since you don't care about the future's value, you could just complete it with null:

future.complete(null);

Upvotes: 38

Related Questions