My Playframework Action Returns Before A Future Is Ready, How Do I Update A Web Page Component?
I have a Scala PlayFramework function that calls MongoDB and gets a Future[Seq[Document]] result. After a map zoom/pan event, this Play Action function is called from JavaScript
Solution 1:
Action
is not designed to work with futures. Use Action.async
, which will "wait" (technically not wait, but schedule) for the future to finish:
def rect(swLon: Float, swLat: Float, neLon: Float, neLat: Float) = Action.async {
val sb = new StringBuilder()
sb.append("<tt>boundingBox: swLon=" + swLon + ", swLat=" + swLat + ", neLon=" + neLon + ", neLat=" + neLat + "</tt>")
if (oDb.isDefined) {
val collection: MongoCollection[Document] = oDb.get.getCollection(collectionName)
val fut = getFutureOne(collection) // returns a Future[Seq[Document]]
fut.map {docs =>
setMongoJson(doc.toJson)
Ok(sb.toString)
} recover {
case e => BadRequest("FAIL: " + e.getMessage)
}
} else Future.successful(Ok("Not defined"))
}
Take a look at this for reference: https://www.playframework.com/documentation/2.4.x/ScalaAsync
Post a Comment for "My Playframework Action Returns Before A Future Is Ready, How Do I Update A Web Page Component?"