Randomize Randomize -4 years ago 190
Scala Question

Scala: Waiting for two futures results

I have two

Future[JsArray] f1,f2
and I need to wait that both are completed with a result. If they both succeed I sum their results. In case one or both fail, the final result is fail.

How can I get that?

Answer Source

The following should do the trick:

for {
  jsArray1 <- f1
  jsArray2 <- f2
} yield JsArray(jsArray1, jsArray2)
  1. Use a for comprehension to sequence the two Futures;
  2. Combine the two JsArrays using this case class's apply method.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download