When should the Promise method return instead of the actual result in the Play Framework?

I am relatively new to the Play Framework. In the current project I'm working on, there are tons of Promise returned by service-level components, right down to the controllers. Interestingly, this is the best practice. In my opinion, using Promise really clutters the sources. And I have to use final modifiers too often so that local variables, parameters, and class members are accessible to anonymous Function I need to create for these Promise s. It even affects how I create my unit test cases. It feels ugly honestly, and there are just too many lines of code than necessary. I'm not even sure that we are doing everything right, I feel that we are abusing Promise s.

I am using Java by the way.

So, when should I use Promise , when should I return Promise , and when should I not use Promise ? Should all our services and interfaces return Promise ? Is there a better way to do this? In plain English, please.

+8
source share
2 answers

Promises are used when you perform an operation that is expensive and relatively time consuming. For example, if you make an intensive query in a database, you do not want your end user to wait while you collect all the records.

In such cases, you return a promise instead of a result. This will stop the external interface from freezing, waiting for a response for intensive computing. It will get a real result after the calculation is complete.

Promises run in a separate thread, so use them only when necessary. In most cases, you just need to use the result.

Play has a nice page! Framework documentation that talks about asynchronous HTTP programming :

A promise will ultimately be redeemed with a value of type Result. Using Promise instead of the usual result, we can quickly return from our action without blocking anything. Then the game will serve the result as soon as the promise is paid.

If you need to transfer data, I will also consider EventSource (for events sent by the server) or WebSockets. I would also recommend watching the latest version of Play (2.3) and getting Java 8 to use Lambda Expressions, which will reduce the verbosity of the code. You can find some examples in here .

Oh, and it looks like you are abusing Promises. . You only need them if you are doing expensive computing and computing resources. Again, if your code looks unreadable due to anonymous inner classes and functions, I highly recommend switching to Java 8 and using lambda expressions .

+5
source share

Although this is an old thread, leaving the answer from the latest documentation on Play to clarify the next question in the comments on how to handle the Promise response on the client side. With Play, the client side will remain blocked while waiting for a response, while the server will not be blocked for I / O if the controller returns Promise. In the end, the promise will be redeemed and the I / O result will be returned.

... we can quickly return from our action without blocking anything. Then the game will serve the result as soon as the promise is paid off.

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

+3
source share

All Articles