如何迭代Future[Seq[]]
并将Int
值添加到Seq
并返回此Seq
?
我的代码如下所示:
def createCopyOfProcessTemplate(processTemplateId: Int): Action[AnyContent] = Action.async {
processTemplateDTO.createCopyOfProcessTemplate(processTemplateId).map { process =>
processTemplateDTO.getProcessStepTemplates(processTemplateId).map { steps =>
steps.foreach(processStep =>
processTemplateDTO.createCopyOfStepTemplates(processTemplateId, process.get.id.get, processStep))
}
// Fetch all Steps which belongs to this Template
val processSteps: Future[Seq[ProcessStepTemplatesModel]] = processTemplateDTO.getProcessStepTemplates(processTemplateId)
val stepIds:Seq[Int]=Seq()
processSteps.map(
steps => steps.foreach(
step => stepIds:+step.id
)
)
Ok(Json.obj("process" -> process, "steps" -> stepIds))
}
}
由于异步性,我认为Seq
是空的。我该如何解决这个问题?
答案 0 :(得分:3)
你可以采取不同的方法:
Action.async {
...
processSteps.map(_.map(_.id))
.map(ids => Ok(Json.obj("process" -> process, "steps" -> ids)))
}