Puneeth Reddy V
Puneeth Reddy V

Reputation: 1578

Scala future fallback method clarification

After seeing this statement on Scala future fallback method I was trying to permutate the success and failure variable values. I'm not able to follow with this case

Future fallbackTo combines 2 Futures into a new Future, and will hold the successful value of the second Future if the first Future fails.

I have a simple code like :

val success = Future{new Exception("Failure from success")}
val failure = Future{'c'}
val fallBackOperation = success.fallbackTo(failure)
val result= Await.result(fallBackOperation, Duration.Inf)
println(result)

This code snippet outcome is

Future(Success(java.lang.Exception: Failure from success))

As per the documentation

I was excepting failure variable as outcome how ever I'm getting the outcome from success variable. Is there anything I'm missing?

Upvotes: 1

Views: 372

Answers (1)

Evgeny
Evgeny

Reputation: 1770

You forgot to throw your exception to fail first future

Upvotes: 4

Related Questions