[英]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
域對象的幫助下getLinked
和getAccountProfile
方法呢?
是的你可以。 我認為這是您在假設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.