Play: should you always use Action.async?

Viewed 344

I'm a little confused about how Play! works in its controller layer. Some documentation states that Play! is always asynchronous and non-blocking. So I'm confused by Action vs Action.async. Is Action blocking? If so, and docs say that you should always stay asynchronous and non-blocking, then should you always use Action.async in all your controllers? Why would you choose not too? I'm sure there's something that I'm not understanding here, and any of your expertise would be greatly appreciated in understanding this.

3 Answers

https://www.playframework.com/documentation/2.8.x/ScalaAsync

Actions are asynchronous by default

Play actions are asynchronous by default. For instance, in the controller code below, the { Ok(...) } part of the code is not the method body of the controller. It is an anonymous function that is being passed to the Action object’s apply method, which creates an object of type Action. Internally, the anonymous function that you wrote will be called and its result will be enclosed in a Future.

def echo = Action { request =>
  Ok("Got request [" + request + "]")
}

Note: Both Action.apply and Action.async create Action objects that are handled internally in the same way. There is a single kind of Action, which is asynchronous, and not two kinds (a synchronous one and an asynchronous one). The .async builder is just a facility to simplify creating actions based on APIs that return a Future, which makes it easier to write non-blocking code.

Related