I am in weird situation of futures , I have this function call executeCall which returns a Future.failed(new Exception("Rts script failed ")) or Future.successful(this) now I am calling this function on a list of string like example shown below
Now I have 2-3 scenarios which I want to understand in the sample code below
- when
executeCallis called on elements ofelemsthen it can either fail or get successful , what I want is that if executing that function returns an failure keep executing theexecuteCallon other elements but in the end execute function should be failed if any of the element fails . - When
executeCallis called on the elments and any of the element returns failure , then code should be sudden exited - If i use the
case 1mentioned as shown below then in that case my code execution get sudden stopped if any of theelemfails - If i use the
case 2, code never fails even when someelemreturn failure not able to understand why not - Correct me on this question . Firstly we are trying to recover any exception thrown from the the end of
executeCallbut after recovering it we are converting it into our own exception and then we are storing the response into a variable - Correct me on this . In case 2 when we use up a underscore what does it check
val elems = Seq("elem1" , "elem2" , "elem3" )
def execute(ctx ): Future[MyContext] = {
val result = elems.map {
script => {
executeCall(script)
.recoverWith { case e =>
logger.error(e, s"Failed to execute [${script}] RTS script")
throw MyOwnException(s"Failed to execute RTS script", e)
}
}
}
// case 1
Future(ctx)
// case 2
Future.sequence(result)
.map(_ => ctx)