Play Framework 2.1 + ReactiveMongo - populates several drop-down lists

I have a form with several drop-down lists that I create using the select and options helpers in the template. Objects that must fill in the lists are taken from the database. However, fetching from the database is asynchronous, so I have to use Async in action, as in this example . However, in my case there is not a single find operation that I should expect, but Seq Future objects of unknown size. So, how can I wait on multiple Future -s to prepare lists before rendering the page? Or maybe there is a better way to do this?

+4
source share
1 answer

Scala futures would be worthless if you hadn’t a good way to combine them.

If you have a sequence of future objects, you can transform it into a future sequence:

 val futureList = Future.sequence(listOfFutures) 

So now you have one future. See Documents A Future Companion for several other useful features for combining futures in various ways.

If you are interested in learning about other ways to play with futures (for example, you can even combine them using simple compilation due to its monadic nature), you can take a look at the primer on Scala futures for more information.

Also, if you are working with ReactiveMongo, it is definitely worth taking a look at the docs on the implementation of Enumerator / Iteratee in Play 2.X. If you master this approach, you can do real magic by combining your reactive data streams on the fly and much more.

+7
source

All Articles