Retrieve value from item wrapped in a Future[Option[Item]] I have a function like this

def findById(id: UUID): Future[Option[Item]]

case class Item(subject: String)


Now I want to retrieve the subject if the item exists else I want the subject to be empty string.

val subject: Future[String] = repository
  .findById(someId)
  .map(_.map(i =&gt; i.subject))
  .map(_.getOrElse(""))

subject.foreach(doSomething)


Am I doing it right or is there a better way in Scala without frameworks like Scalaz.
It's better that you are staying in the Futures domain, no longer trying to unwrap to String.

It's not so easy to follow all of the underscore-based function expressions. I'd try something like...

val subject: Future[String] = {
  repository.findById(someId) map { mbItem =&gt;
    mbItem match {
      case Some( item ) =&gt; item.subject
      case None         =&gt; ""
    }
  }
}