简体   繁体   中英

scala nested for comprehension with futures

My case domain classes are as below

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

my access layer implementation signature to extract above domain classes look like below

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

Can I have a for comprehension to construct Future list of AccountInfo domain object with the help of getLinked and getAccountProfile methods ?

Yes you can. I think this is what you're looking for assuming AccountProfiles.actId and AccountInfo.id are supposed to be equivalent.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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