Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up
Find file
Copy path
Scala-High-Performance-Programming/chapter6/src/main/scala/highperfscala/concurrency/future/SafeAwait.scala
Find file
Copy path
Fetching contributors…
| package highperfscala.concurrency.future | |
| import java.util.concurrent.TimeoutException | |
| import scala.concurrent.duration.Duration | |
| import scala.concurrent.{Awaitable, Await} | |
| import scala.util.{Failure, Success, Try} | |
| object SafeAwait { | |
| def result[T]( | |
| awaitable: Awaitable[T], | |
| atMost: Duration): Option[T] = Try(Await.result(awaitable, atMost)) match { | |
| case Success(t) => Some(t) | |
| case Failure(_: TimeoutException) => None | |
| case Failure(e) => throw e | |
| } | |
| } |