title: Run multiple futures in parallel, return default value on timeout
body: <p>I have to run multiple futures in parallel and the program shouldn't crash or hang.</p>
  
  <p>For now I wait on futures one by one, and use fallback value if there is TimeoutException.</p>
  
  <pre><code>val future1 = // start future1
  val future2 = // start future2
  val future3 = // start future3
  
  // &lt;- at this point all 3 futures are running
  
  // waits for maximum of timeout1 seconds
  val res1 = toFallback(future1, timeout1, Map[String, Int]())
  // .. timeout2 seconds 
  val res2 = toFallback(future2, timeout2, List[Int]())
  // ... timeout3 seconds
  val res3 = toFallback(future3, timeout3, Map[String, BigInt]()) 
  
  def toFallback[T](f: Future[T], to: Int, default: T) = {
    Try(Await.result(f, to seconds))
      .recover { case to: TimeoutException =&gt; default }
  }
  </code></pre>
  
  <p>As I can see, maximum wait time of this snippet is <code>timeout1 + timeout2 + timeout3</code></p>
  
  <p>My question is: how can I wait on all of those futures at once, so I can reduce wait time to <code>max(timeout1, timeout2, timeout3)</code>?</p>
  
  <p>EDIT: In the end I used modification of @Jatin and @senia answers:</p>
  
  <pre><code>private def composeWaitingFuture[T](fut: Future[T], 
                                      timeout: Int, default: T) =
    future { Await.result(fut, timeout seconds) } recover {
      case e: Exception =&gt; default
    }
  </code></pre>
  
  <p>and later it's used as follows:</p>
  
  <pre><code>// starts futures immediately and waits for maximum of timeoutX seconds
  val res1 = composeWaitingFuture(future1, timeout1, Map[String, Int]())
  val res2 = composeWaitingFuture(future2, timeout2, List[Int]())
  val res3 = composeWaitingFuture(future3, timeout3, Map[String, BigInt]()) 
  
  // takes the maximum of max(timeout1, timeout2, timeout3) to complete
  val combinedFuture =
    for {
      r1 &lt;- res1
      r2 &lt;- res2
      r3 &lt;- res3
    } yield (r1, r2, r3)
  </code></pre>
  
  <p>and later I use <code>combinedFuture</code> as I see fit.</p>