前面的示例演示了Future的各个功能,分别处理成功和失败案例。但是,通常,这两个功能的处理要简洁得多。这是一个以更简洁,更实际的方式编写的示例:
object Calculator { def calculateAndReport(a: Int, b: Int) = { val eventualQuotient = FutureDivider divide(a, b) eventualQuotient map { quotient => println(s"Quotient: $quotient") } recover { case ex: ArithmeticException => println(s"It failed with: ${ex.getMessage}") } } }