繁体   English   中英

嵌套以了解期货的scala

[英]scala nested for comprehension with futures

我的案例域类如下

case class Account(randomId: String, accounts: List[String]) // for each of accounts i need to get AccountProfiles.

case class AccountProfiles(actId: String, profiles: List[String], additionalInfo: Map[String, String], ......)

case class AccountInfo(id: String, profiles:List[String]) // for each of AccountProfiles I need to construct AccountInfo

我的访问层实现签名来提取上面的域类,如下所示

getLinked(): Future[Account]
getAccountProfile(actId: String): Future[AccountProfiles]

我能for理解来构建Future的列表AccountInfo域对象的帮助下getLinkedgetAccountProfile方法呢?

是的你可以。 我认为这是您在假设AccountProfiles.actId和AccountInfo.id应该等效的情况下所寻找的东西。

for {
  account <- getLinked()
  profiles <- Future.sequence(account.accounts map { id => getAccountProfile(id) })
} yield profiles map { p => AccountInfo(p.actId, p.profiles) }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM