Books

Until now, we were able to compute the result to send to the web client directly. This is not always the case: the result may depend of an expensive computation or on a long web service call.

Because of the way Play 2.0 works, action code must be as fast as possible (i.e. non blocking). So what should we return as result if we are not yet able to compute it? The response should be a promise of a result!

A Promise<Result> will eventually be redeemed with a value of type Result. By giving a Promise<Result> instead of a normal Result, we are able to compute the result quickly without blocking anything. Play will then serve this result as soon as the promise is redeemed.

The web client will be blocked while waiting for the response but nothing will be blocked on the server, and server resources can be used to serve other clients.

Note: Writing functional composition in Java is really verbose for the moment, but it should be better when Java supports lambda notation.

Play 2.0 asynchronous API methods give you a Promise. This is the case when you are calling an external web service using the play.libs.WS API, or if you are using Akka to schedule asynchronous tasks or to communicate with Actors using play.libs.Akka.

A simple way to execute a block of code asynchronously and to get a Promise is to use the play.libs.Akka helpers: