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.
Play is fully asynchronous and non-blocking.
Most actions tend to require being executed inline and so Play conveniently reflects this. However if you have an async operation within your action (calling WS for example) then you use Action.async to pass back the future result.
Action
is blocking and it takes a function from Request=>Result
, so you can do any blocking stuff in it. Action.async
is non-blocking and requires a function from Request=>Future[Result]
but here it is important to realize that only non-blocking stuff in it has matter, because if you block some where further it would be pointless. WS call mentioned by @Christopher Hunt is a good example because it gives you a Future[Response] that you can easily transform to Future[Result]
what is expected by Action.async as a return type. But you are free to use scala.Future API
for any stuff that might be async & non-blocking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With