To understand the future with exceptions

I have two Futures and I want to restore exceptions on one of them:

def getId(): Future[Either[Exception, Id]] = ...
def result(): Future[Result] = ...

      

I'll try something like this:

for {
  id <- getId()
  r <- result(id, param, param)
} yield {
  r
} recover {
  case e => println(e.getMessage)
}

      

Can anyone help me?

Thank you so much.

+3


source to share


1 answer


You should restore in the processed shaft, not the for. In the code below, id and r values ​​are 0 if an exception is thrown.



for {
  id = getId()
  id.recover {
    case e => {
      println(e.getMessage)
      0
    }
  }
  r = result(id, param, param)
} yield r.recover {
    case e => {
      println(e.getMessage)
      0
    }
  }

      

0


source







All Articles