How can I make A future of future into one future object?

前端 未结 3 1744
再見小時候
再見小時候 2020-12-14 08:16

Env: Akka 2.1, scala version 2.10.M6, JDK 1.7,u5

Now is my problem: I have:

future1 = Futures.future(new Callable>(){...         


        
3条回答
  •  醉梦人生
    2020-12-14 08:44

    Note: Since Viktor Klang's 2012 answer, he recently (March 2016) added in his blog for scala 2.12:

    Missing canonical combinators: flatten

    Are you one of us Future-users who have grown tired of the old flatMap(identity) boilerplate for un-nesting Futures as in:

    val future: Future[Future[X]] = ???
    val flattenedFuture /*: Future[X] */ = future.flatMap(identity)
    

    Then I have some great news for you! Starting with Scala 2.12
    scala.concurrent.Future will have a flatten-method with the following signature:

    def flatten[S](implicit ev: T <:< Future[S]): Future[S]
    

    Allowing you to write:

    val future: Future[Future[X]] = ???
    val flattenedFuture /*: Future[X] */ = future.flatten
    

提交回复
热议问题