Search code examples
scalaasynchronousakkamustachescalatra

How to handle multiple async requests for a Mustache template on Scalatra


I have a simple web app running on Scalatra with Mustache templates. In order to render the page, I need to make three separate requests to web services. Is the Akka approach shown in Scalatra guides the way to go? Do I need to introduce chaining or latches? Or is it possible to pass results to Mustache template as they arrive?


Solution

  • This is what I ended up using.

    new AsyncResult {
    
      val animals = for{
        r1 <- service.getCats()
        r2 <- service.getDogs()
        r3 <- service.getPonies()
      } yield (r1, r2, r3)
    
      val is = animals map (result => mustache("/template.mustache", "cats" -> result._1, "dogs" -> result._2, "ponies" -> result._3))
    }