如何将 Future[Vector[UserLocation]] 转换为 Future[Vector[User]]

How to convert a Future[Vector[UserLocation]] to a Future[Vector[User]]

下面有一个方法我想 return Future[Vector[user]].

方法userLocationService.getUserLocationsInList将return一个Future[Vector[UserLocation]]

UserLocation 看起来像这样:

case class UserLocation(id: Int, locationId: Int, userId: Int)


def getUsersInLocation(locationIdList: Set[Int]): Future[Vector[User]] = {

   userLocationService.getUserLocationsInList(locationIdList).map{
      userLocations =>
          // ????????????
   }

}

我有一个方法 return 是基于 UserId 的单个用户,例如:

userService.getById(userId: Int): Future[User]

如何根据以上内容构建 Future[Vector[User]]?

如果你 map Future[Vector[UserLocation]],你可以很容易地从包含的 Vector[UserLocation]:

中生成一个 Vector[Future[User]]
userLocations.map(location => userService.getById(location.userId))

您可以使用 Future.sequenceVector[Future[User]] 反转为 Future[Vector[User]]:

Future.sequence(userLocations.map(location => userService.getById(location.userId)))

或使用Future.traverse:

Future.traverse(userLocations) { location => userService.getById(location.userId) }

这会给您留下 Future[Future[Vector[User]]],可以通过将 map 更改为 flatMap 来解决这个问题。把它们放在一起:

def getUsersInLocation(locationIdList: Set[Int]): Future[Vector[User]] = {
  userLocationService.getUserLocationsInList(locationIdList).flatMap { locations =>
    Future.traverse(locations) { location =>
      userService.getById(location.userId)
    }
  }
}

或者用一个for-comprehension:

def getUsersInLocation(locationIdList: Set[Int]): Future[Vector[User]] = {
  for {
    locations <- userLocationService.getUserLocationsInList(locationIdList)
    users <- Future.traverse(locations) { location =>
      userService.getById(location.userId)
    }
  } yield users
}