Skip to content
Permalink
Fetching contributors…
Cannot retrieve contributors at this time
17 lines (14 sloc) 493 Bytes
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
}
}
You can’t perform that action at this time.