本文介绍了playframework scala Seq [Future [Model]]移除未来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下问题,
我有一个Seq[Future[ProcessStepTemplatesModel]]
,我想删除内部的Future.
I have a Seq[Future[ProcessStepTemplatesModel]]
and I want to remove the inner Future.
我期望得到的是Seq[ProcessStepTemplatesModel]
.
完整的代码如下:
Future.sequence {
processSteps.map { step => // This is a "for each" over a Seq[ProcessStepTemplatesModel]
val prerequisiteFuture = processStepPrerequisitesDTO.getProcessStepPrerequisiteProcessTemplateIds(step.id.get)
prerequisiteFuture.map(prereqTemplates => {
processTemplateDTO.getProessTemplatesForStepPreqrequsites(prereqTemplates).map(pres => {
step.stepPrerequisites ++Some(pres)
step
})
})
}
}
谢谢
更新:
def getEditProcessTemplateData(processTemplateId: Int) = Action.async {
//Get all steps of this process templates
val stepIds: Future[Seq[Int]] = processTemplateDTO.getProcessStepTemplateIds(processTemplateId)
val process = for {
allApprovedProcessTemplates <- processTemplateDTO.getApprovedProcessTemplates //Get all approved process templates
processTemplate <- processTemplateDTO.getProcessTemplate(processTemplateId) // Get the Process Template
prerequisites <- getProcessTemplateForEdit(processPrerequisitesDTO.getProcessPrerequisiteProcessTemplateIdsByProcessTemplateId(processTemplateId))
postConditions <- getProcessTemplateForEdit(processPostConditionsDTO.getProcessPostConditionProcessTemplateIdsByProcessTemplateId(processTemplateId))
approvedProcessTemplate <- processTemplateDTO.getProcessTemplate(processTemplate.get.approveprocess)
trainedProcessTemplate <- processTemplateDTO.getProcessTemplate(processTemplate.get.trainingsprocess)
processSteps <- processTemplateDTO.getProcessStepTemplates(processTemplateId)
// Step prerequisites
processStepsPrerequisites <- getProcessStepsPrerequisites(stepIds)
processStepsPrerequisiteProcessTemplate <- getProcessStepsPrerequisiteProcessTemplate(stepIds)
processTemplatesForStepPrerequisites <- getProcessTemplateForStepPrerequisite(stepIds)
// Step post conditions
processStepsPostConditions <- getProcessStepsPostConditions(stepIds)
processStepPostConditionProcessTemplate <- getProcessStepPostConditionProcessTemplate(stepIds)
processTemplatesForStepPostConditions <- getProcessTemplateForStepPostCondition(stepIds)
// Derived processes
derivedProcesses <- getDerivedProcesses(stepIds)
processTemplatesForStepDerivedProcesses <- getProcessStepsDerivedProcesses(stepIds)
// Process to process step
processStepsTemplates_ProcessTemplates <- getProcessStepsTemplates_ProcessTemplates(stepIds)
processTemplatesForProcessTemplatesToProcessStep <- getProcessTemplateToProcessStepId(stepIds)
responsible <- raciProcessTemplateDTO.getResponsibleProcessTemplates(processTemplateId) // get all responsibles for this process template
accountable <- raciProcessTemplateDTO.getAccountableProcessTemplates(processTemplateId) // get all accountables for this process template
consulted <- raciProcessTemplateDTO.getConsultedProcessTemplates(processTemplateId) // get all consulted for this process template
informed <- raciProcessTemplateDTO.getInformedProcessTemplates(processTemplateId) // get all consulted for this process template
} yield (allApprovedProcessTemplates, processTemplate, prerequisites, postConditions, processSteps, processStepsPrerequisites,
processStepsPrerequisiteProcessTemplate, processTemplatesForStepPrerequisites, processStepsPostConditions, processStepPostConditionProcessTemplate, processTemplatesForStepPostConditions, derivedProcesses,
processTemplatesForStepDerivedProcesses, processStepsTemplates_ProcessTemplates, processTemplatesForProcessTemplatesToProcessStep, approvedProcessTemplate, trainedProcessTemplate, responsible, accountable, consulted, informed)
process.flatMap({ case (allApprovedProcessTemplates, processTemplate, prerequisites, postConditions, processSteps, processStepsPrerequisites,
processStepsPrerequisiteProcessTemplate, processTemplatesForStepPrerequisites, processStepsPostConditions, processStepPostConditionProcessTemplate, processTemplatesForStepPostConditions, derivedProcesses,
processTemplatesForStepDerivedProcesses, processStepsTemplates_ProcessTemplates, processTemplatesForProcessTemplatesToProcessStep, approvedProcessTemplate, trainedProcessTemplate, responsible, accountable, consulted, informed) =>
Future.sequence {
processSteps.map {
step =>
val prerequisiteFuture = processStepPrerequisitesDTO.getProcessStepPrerequisiteProcessTemplateIds(step.id.get)
prerequisiteFuture.map(prereqTemplates => {
processTemplateDTO.getProessTemplatesForStepPreqrequsites(prereqTemplates).map(pres => {
println("test", pres) // they are printed
step.stepPrerequisites ++Some(pres)
println("test2", step)
step
})
})
}
}.map {
stepTemplates =>
Ok(Json.obj(
"allApprovedProcessTemplates" -> allApprovedProcessTemplates,
"processTemplate" -> processTemplate,
"prerequisites" -> prerequisites,
"postConditions" -> postConditions,
"approvedProcessTemplate" -> approvedProcessTemplate,
"trainedProcessTemplate" -> trainedProcessTemplate,
// Step prerequisites
"processStepsPrerequisites" -> processStepsPrerequisites,
"processStepsPrerequisiteProcessTemplate" -> processStepsPrerequisiteProcessTemplate,
"processTemplatesForStepPrerequisites" -> processTemplatesForStepPrerequisites,
// Step post conditions
"processStepsPostConditions" -> processStepsPostConditions,
"processStepPostConditionProcessTemplate" -> processStepPostConditionProcessTemplate,
"processTemplatesForStepPostConditions" -> processTemplatesForStepPostConditions,
// Derived processes
"derivedProcesses" -> derivedProcesses,
"processTemplatesForStepDerivedProcesses" -> processTemplatesForStepDerivedProcesses,
// Process to process step
"processStepsTemplates_ProcessTemplates" -> processStepsTemplates_ProcessTemplates,
"processTemplatesForProcessTemplatesToProcessStep" -> processTemplatesForProcessTemplatesToProcessStep,
"steps" -> "stepTemplates",
"responsible" -> responsible,
"accountable" -> accountable,
"consulted" -> consulted,
"informed" -> informed
))
}
})
}
推荐答案
有效的解决方案是:
Future.sequence {
processSteps.map {
step =>
val prerequisiteFuture = processStepPrerequisitesDTO.getProcessStepPrerequisiteProcessTemplateIds(step.id.get)
prerequisiteFuture.map(prereqTemplates => {
processTemplateDTO.getProessTemplatesForStepPreqrequsites(prereqTemplates).map(pres => {
println("test", pres) // they are printed
step.stepPrerequisites = Some(pres)
println("test2", step)
step
})
})
}
}.flatMap {
stepTemplates => {
Future.sequence(stepTemplates).map(test =>
Ok(Json.obj( [....]
这篇关于playframework scala Seq [Future [Model]]移除未来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!