I've got a function in which I'd like to return a seq of elements like this:
def getProcessPrerequisites(processTemplateId: Int): Seq[ProcessTemplatesModel] = { var processTemplates: Seq[ProcessTemplatesModel] = Seq() //Other function calls... processTemplateIds.foreach(processTemplateId => processTemplateDTO.getProcessTemplate(processTemplateId).map { case Some(processTemplate) => processTemplates = processTemplates :+ processTemplate println("List in foreach: " + processTemplates) case None => println("Process template not found: " + processTemplateId) case _ => println("Something went wrong processTemplateDTO.getProcessTemplate(" + processTemplateId + ")!") }) println("List in the end: " + processTemplates) processTemplates } Eventually, I iterate over a list of processTemplateIds in order to get the corresponding elements. However, scala decides, probably due to its asynchronous nature, to return an empty list in the end. The println within the foreach:
println("List in foreach: " + processTemplates) reveals that the list is not empty and is properly filled. Problem is, that it happens after this println:
println("List in the end: " + processTemplates) which is empty. How can I assure that the list is not empty or waits for the list to be filled? Am I doing something wrong? Thanks!
UPDATE
var test = "" processPrerequisitesDTO.getProcessPrerequisites(processTemplateId).map { processPrerequisiteIds => { test += " hello " processPrerequisiteIds.foreach(processTemplateId => processTemplateDTO.getProcessTemplate(processTemplateId).map { case Some(processTemplate) => processTemplates += processTemplate test += " hi " }) println("FirstPrint: " + test) } } println("SecondPrint: " + test) The "FirstPrint" says "ha" the "SecondPrint" is empty. Does this have to do with scoping? I don't get it, not even the " hi " is added...