monads - Scala Futures - flatMap and onFailure -
if have computation takes while might place in scala.concurrent.future:
val f = future { somelongrunningfunction() } and let's want else asynchronously once computation completed:
f.flatmap{ _ => anotherlongrunningfunction() } in event f's initial block fails, how "idiomatically" handle when using flatmap or other combinators? merely case of using recover or onfailure before flatmap?
i elegance , simplicity of using flatmap seems failure scenarios in way of it.
edit: second future reliant on first, hence flatmap. i'm looking solution that'll elegantly let me chain flatmap handle failures of first.
to quote scaladoc flatmap:
creates new future applying function to successful result of future, , returns result of function new future. if future completed exception new future contain exception.
notice bold, meaning whatever pass flatmap executed if initial future completes successfully.
so should handle result of entire execution:
val result = future1.flatmap { result => functionreturningfuture2(result) } and then:
result.onfailure // or result.onsuccess // or result.recover
Comments
Post a Comment