Scala Future – 为了理解,混合同步和异步

前端之家收集整理的这篇文章主要介绍了Scala Future – 为了理解,混合同步和异步前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的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,并且我不想仅仅为了返回已经获取的值而进行额外的异步调用.

解决方法

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(_))

猜你在找的Scala相关文章