Await a future, receive an either

You could use Await.ready which simply blocks until the Future has either succeeded or failed, then returns a reference back to that Future.

From there, you would probably want to get the Future’s value, which is an Option[Try[T]]. Due to the Await.ready call, it should be safe to assume that the value is a Some. Then it’s just a matter of mapping between a Try[T] and an Either[Throwable, T].

The short version:

val f: Future[T] = ...

val result: Try[T] = Await.ready(f, Duration.Inf).value.get

val resultEither = result match {
  case Success(t) => Right(t)
  case Failure(e) => Left(e)
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)