Question

I want to take a function that returns a Future[Option[String]] and use that in conjunction with spray routing's onComplete directive. But no matter what I do, I can't seem to get it work.

Let's say that I have the following function:

def expensiveOperation: Future[Option[String]] = { ... do stuff ... }

And then I want to define a portion of my Route as such:

onComplete(expensiveOperation) {
  case Success(string) => complete(string)
  case Failure(_) => complete("failure")
}

Is there a way to do this without writing a separate function to transform the Future[Option[String]] into a basic Future[String]?

Was it helpful?

Solution

onComplete(expensiveOperation) {
  case Success(Some(string)) => complete(string)
  case _ => complete("failure")
}

or:

onComplete(expensiveOperation.map(_.get)) {
  case Success(string) => complete(string)
  case Failure(_) => complete("failure")
}

OTHER TIPS

A Late Answer. Found this to be working.

post{
      entity(as[Project]) { project =>
          complete {
            (projectActor ? Update(project)).mapTo[Project]
          }
      }
   }

Hope this Fixes the issue

https://groups.google.com/forum/#!topic/spray-user/FM6mF6JXuNM

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top