Chain Scala期货回归类型
发布时间:2020-12-16 08:50:05 所属栏目:安全 来源:网络整理
导读:我试图在 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
我试图在
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]]的错误 如何正确链接它们? 解决方法
我会改用flatMap.
def getUUIDRecordByModel(x:DirectFlight): Future[Option[FlightByUUIDModel]] = { getOneRecordByModel(x) flatMap { case Some(flight) => getRecordByUUID(flight) case None => Future.successful(None) } } 然后应用副作用函数并返回原始的Future,而不是内部的Future. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |