scala-async
scala-async copied to clipboard
Document how error handling works
How are failed futures handled? Does the whole async
block just fail? Is there a way to handle individual failed await
s within one async
block, or is the only solution there to nest async
blocks? If that's the case, perhaps a tryAwait
that returns a Try
would be helpful. In any case, a couple examples in the docs would be great. Thanks
I would also be interested in this.
The code behaves as if the call to await
throws the exception with which the awaited Future failed.
While await
under try/catch is currently unsupported, one alternative would be to write await(future recover ...)
.
tryAwait
would be equivalent to this simple function: def tryAwait[T](fut: Future[T]): Try[T] = await(fut map Success.apply recover PartialFunction(Failure.apply)
However, that won't work because await
can't be used inside a function; a macro is needed. I'll submit a PR with that macro if you'd be interested in including tryAwait
in the project.
(From what I've read of the code, most of the work will be to construct the right kind of Tryy. Why is there even a Tryy type if both implementations use scala.util.Try?)
+1 to tryAwait at least until proper try/catch handling is supported
I'll submit a PR with that macro if you'd be interested in including tryAwait in the project.
This would be much appreciated!
I was playing with async/await and this is one of the first questions that popped up. So :+1: to improve this.
FWIW I used @danarmak's solution above:
await(kittens() map Success.apply recover PartialFunction(Failure.apply)) match {
case Failure(t) ⇒ // ...
case Success(v) ⇒ // ...
}
But a tryAwait
shortcut would be good.