Created
June 17, 2016 07:19
-
-
Save rethab/9fc5395036835aaa1655e82e2af9ab1f to your computer and use it in GitHub Desktop.
Awaiting a scala future for completion w/o the Await.result, which may spawn a new thread.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private def block[T](f: Future[T]): T = { | |
val barrier = new CountDownLatch(1) | |
import play.api.libs.concurrent.Execution.Implicits.defaultContext | |
f onComplete {_ => | |
barrier.countDown() | |
} | |
barrier.await(30, TimeUnit.SECONDS) | |
f.value.getOrElse{ | |
throw new RuntimeException("Future was None: " + f.isCompleted) | |
}.get | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment