Someone pointed out that the reason the code was returning early was because the innermost getTree() returns a Future immediately (Doh!). It should have a map and enclose the two lines below it inside the map.
The else
was changed to Future.successful and the getEntitiesFromChildEdges( edges).map
was changed to flatMap
.
Here's the updated code.
getTree( rootEntities: Seq[Entity], parents: Seq[Entity], depth: Int): Future[Seq[Entity]] {
getEdges( parents).flatMap{ edges =>
getEntitiesFromChildEdges( edges).flatMap{ children =>
children.foreach{ child => findParent( child).addChild( child) }
if( depth > 0) {
// getTree needs to have a map and the results inside it.
getTree( rootEntities, children, depth - 1).map{ r =>
logTheTree( "DEEPER", rootEntities)
rootEntities
}
} else {
logTheTree( "!DEEPER", rootEntities)
Future.successful( rootEntities)
}
}
}
}
The answer from cmbaxter show promise :-) I'll look into using that technique in the future.