- , :
def doSimpleWork = Future{
true
}
def doComplexWork = Future{
Some("result")
}
val fut1 = doSimpleWork
val fut2 = doSimpleWork
val fut = for{
f1Result <- fut1
f2Result <- fut2
if (f1Result && f2Result)
f3Result <- doComplexWork
if (f3Result.isDefined)
f4Result <- doSimpleWork
} yield "success"
fut onComplete{
case Success(value) => println("I succeeded")
case Failure(ex) => println("I failed: " + ex.getMessage)
}
"" "" , :
fut.recover{case ex => "failed"} onSuccess{
case value => println(value)
}
, , . , Futures, async. doSimpleWork boolean success/fail. doComplexWork - ( ) [String], . doSimpleWork . for comp fut1 fut2 ( ), , . , , fut val NoSuchElementException, , comp for. , doComplexWork . , Some, doSimpleWork. , "success". fut val, Future[String].
From there, we use one of the async callback functions to check whether the entire sequence of calls went completely (case Success), or failed somewhere in the process (case Failure), printing something related to the case in which it got . In the alternative end block of code, we recover from any possible failure by returning String "failed", and then use only a callback onSuccessthat will print either "success" or "unsuccessful" depending on what happened.
source
share