【发布时间】:2014-09-05 03:52:50
【问题描述】:
我正在尝试在 Scala 中链接 Futures,但它给了我错误的返回类型。
我有以下方法:
def getOneRecordByModel(x:DirectFlight): Future[Option[FlightByDetailModel]] = {
select.allowFiltering().where(_.from eqs x.from).and(_.to eqs x.to).and(_.departure eqs x.departure).and(_.arrival eqs x.arrival).and(_.carrier eqs x.airline).and(_.code eqs x.flightCode).one()
}
def getRecordByUUID(x:FlightByDetailModel): Future[Option[FlightByUUIDModel]] = {
select.allowFiltering().where(_.uuid eqs x.uuid).one()
}
def getUUIDRecordByModel(x:DirectFlight): Future[Option[FlightByUUIDModel]] = {
getOneRecordByModel(x) andThen {
case Success(Some(flight)) => getRecordByUUID(flight)
case Success(x) => Success(x)
case Failure(x) => Failure(x)
}
}
但现在我得到getUUIDRecordByModel返回类型为Future[Option[FlightByDetailModel]]的错误
如何正确链接它们?
【问题讨论】:
-
当你使用 andThen 时你不会改变返回类型。您需要 flatMap 或 map 取决于其他方法的返回类型。
-
andThen组合子纯粹是为了产生副作用。它总是返回被调用的Future,保持不变。正如其他人所提到的,map和/或flatMap应该是您正在寻找的。span>