Drawing up the future and trying

This is a follow-up to my previous question

Suppose I have a function that returns a Future[String]

and a function String => Try[Int]

:

val slowAsync : Int => Future[String] = ...
val mayFail   : String => Try[Int] = ...

      

Suppose I compose these to create a new function Int => Future[Int]

:

val composed : Int => Future[Int] = {x => 
  for (str <- slowAsync(x); y <- Future(mayFail(s).get)) yield y
} 

      

It might work composed

, but I don't like it Future(mayFail(s).get)

. How do you fix this?

+3


source to share


1 answer


You can use fromTry:



val x = slowAsync(1) flatMap (s => Future.fromTry(mayFail(s)))

      

+5


source







All Articles