In my method1, I need to asynchronously call another method2 that returns Option (result1). Than if result1 is empty, I need to call another method3 asynchronously, but if result1 is not empty, I just need to return it.
Here is a way:
def signIn(username: String): Future[User] = {
for {
foundUser <- userService.findByUsername(username) // this method returns Future[Option[User]],
// foundUser is Option[User]
user <- if (foundUser.isEmpty) {
val newUser = User(username = "User123")
userService.create(newUser).map(Some(_)) // this method returns Future[Option[User]]
}
else
// Here I want to return just foundUser, of course, it is not possible.
// IS THIS APPROACH CORRECT?? DOES THIS LINE CREATE ASYNCHRONOUS CALL?
Future.successful(foundUser)
} yield user
}
Question:
Future.successful(foundUser)Is this approach correct in the code above? Does this line create an asynchronous call? If so, how can this be avoided? I already selected foundUser asynchronously, and I do not want the additional asynchronous call to return the already selected value.
source
share