title: Scala/Play/Akka: delay action response
body: <p>I have a simple Play application where a controller action calls model method which does some calculations and returns the result. What I need is action response to be delayed randomly between 1-5 seconds. I have little experience with Akka, but I don't know how to accomplish what I need.</p>
  
  <p>Example controller:</p>
  
  <pre><code>package controllers
  
  import scala.util.Random    
  import play.api._
  import play.api.mvc._
  
  import models.MyModel
  
  object Application extends Controller {
  
      def calculate = Action { implicit request =&gt;
  
          val result = MyModel.calculate()
          val delay = Random.nextInt(6)
  
          //This response must be delayed somehow
          Ok(result)
  
      }
  
  }
  </code></pre>
  
  <p>Example model:</p>
  
  <pre><code>package models
  
  object MyModel {
  
      def calculate = {
  
          //calculation happens here
  
      }
  
  }
  </code></pre>
  
  <p><strong>UPDATE - SOLUTION</strong> senia's answer is working, but with deprecation warning. This code agrees with Play 2.2.1:</p>
  
  <pre><code>import play.api.libs.concurrent.Promise
  import play.api.libs.concurrent.Execution.Implicits.defaultContext
  
  def calculate = Action.async {
  
      val result = MyModel.calculate()
      val delay = Random.nextInt(6)
      val delayed = Promise.timeout(result, delay.second)
      delayed.map(Ok(_))
  
  }
  </code></pre>