Map a Future for both Success and Failure

后端 未结 4 2037
闹比i
闹比i 2021-01-30 22:38

I have a Future[T] and I want to map the result, on both success and failure.

Eg, something like

val future = ... // Future[T]
val mapped = future.mapAll         


        
4条回答
  •  囚心锁ツ
    2021-01-30 23:06

    Since Scala 2.12 you can use transform to map both cases:

    future.transform {
          case Success(_) => Try("OK")
          case Failure(_) => Try("KO")
    }
    

    You also have transformWith if you prefer to use a Future instead of a Try. Check the documentation for details.

提交回复
热议问题