Below is this code snippet working well but with Await.result
def validate(loginRequest: LoginRequest): Option[AuthResponse] = {
val response:Option[User] = Await.result(userDao.getUserByUsernameAndPassword(loginRequest.username,Utilities.encrypt(loginRequest.password)),Duration.Inf )
response match {
case Some(value) =>populateResponse(value)
case None => None
}
}
I want to use Futures instead of await but then return the response. below is a non blocking snippet
val response:Future[Option[User]]= userDao.getUserByUsernameAndPassword(loginRequest.username,Utilities.encrypt(loginRequest.password))
How would u get a similar response without blocking
response match {
case Some(value) =>populateResponse(value)
case None => None
}
You just need to call map
on the Future
to change the result, and inside that call map
on the Option
to change the contents of the option:
response.map(_.map(populateResponse))
If the Option
is None
then the second map
will do nothing, and if the Future
had failed then the first map
will do nothing.