在我的method1中,我需要异步调用另一个方法2,它返回Option(result1)。比如果result1为空,我需要异步调用另一个方法3,但如果result1不为空,我只需要返回它。
以下是方法:
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
}
问题是:
Future.successful(foundUser)
- 这种方法在上面的代码中是否正确?这行是否会创建异步调用?如果是这样,如何避免呢?我已经异步获取 foundUser ,并且我不想仅仅为了返回已经获取的值而进行额外的异步调用。
答案 0 :(得分:4)
Future.successful
不会在提供的ExecutionContext
上排队其他功能。它只是使用Promise[T]
来创建完整的Future[T]
:
/** Creates an already completed Future with the specified result.
*
* @tparam T the type of the value in the future
* @param result the given successful value
* @return the newly created `Future` instance
*/
def successful[T](result: T): Future[T] = Promise.successful(result).future
作为旁注,您可以使用Option.fold
:
def signIn(username: String): Future[User] =
userService
.findByUsername(username)
.flatMap(_.fold(userService.create(User(username = "User123")))(Future.successful(_))
答案 1 :(得分:1)
@Yuval Itzchakov回答了您的问题,但作为旁注,您可能希望在您的案例中直接使用flatMap
模式匹配。我个人觉得它更具可读性:
def signIn(username: String): Future[User] =
userService.findByUsername(username)
.flatMap {
case Some(user) => Future.successful(user)
case _ => userService.create(User(username = "User123"))
}