Randomize Randomize - 3 months ago 19
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

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.