简体   繁体   中英

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.

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

You can return a Future that holds a value of Unit

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

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