TeeKai
TeeKai

Reputation: 691

Is a way to return Future[Unit]?

I have a method returning Unit originally. After changing returned type to Future[Unit], I can't find out a way to change the method body. The least line is a method call.

Upvotes: 6

Views: 12048

Answers (2)

Ali H.
Ali H.

Reputation: 371

In Scala 2.12 the built-in Future.unit is the way to go. It has the benefit of being cached.

Upvotes: 25

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23329

You can return a Future that holds a value of Unit

def doSomething():Future[Unit] = {
  TimeUnit.SECONDS.sleep(3)
  println("hi")
  Future.successful(())
}

Upvotes: 7

Related Questions