This is a follow-up to my previous question
Suppose I have a function, which returns a Future[String], and a function String => Try[Int]:
val slowAsync : Int => Future[String] = ...
val mayFail : String => Try[Int] = ...
Suppose I am composing them to make a new function Int => Future[Int]:
val composed : Int => Future[Int] = {x =>
for (str <- slowAsync(x); y <- Future(mayFail(s).get)) yield y
}
The composed is probably working but I don't like this Future(mayFail(s).get). How would you fix it?
You can use fromTry:
val x = slowAsync(1) flatMap (s => Future.fromTry(mayFail(s)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With