title: How do I wait for a Scala future's onSuccess callback to complete?
body: <p>In Scala, I can use <code>Await</code> to wait for a future to complete.  However, if I have registered a callback to run upon completion of that future, how can I wait not only for the future to complete but also for that callback to finish?</p>
  
  <p>Here is a minimal but complete program to illustrate the problem:</p>
  
  <pre><code>import scala.concurrent.ExecutionContext.Implicits.global
  import scala.concurrent.duration.Duration
  import scala.concurrent.{ Await, Future }
  
  object Main {
    def main(args: Array[String]): Unit = {
      val f: Future[Int] = Future(0)
      f.onSuccess { case _ =&gt;
        Thread.sleep(10000)
        println("The program waited patiently for this callback to finish.")
      }
  
      // This waits for `f` to complete but doesn't wait for the callback
      // to finish running.
      Await.ready(f, Duration.Inf)
    }
  }
  </code></pre>
  
  <p>I expect the output to be:</p>
  
  <pre><code>The program waited patiently for this callback to finish.
  </code></pre>
  
  <p>Instead, there is no output; the program exits before the callback finishes.</p>
  
  <p>Please note that this is not the same problem as waiting for a future to complete, which has been answered previously at <a href="https://stackoverflow.com/questions/10565475/possible-bug-in-scala-2-10-futures-do-not-run">this question</a>.</p>