Skip to content

Instantly share code, notes, and snippets.

@rethab
Created June 17, 2016 07:19
Show Gist options
  • Save rethab/9fc5395036835aaa1655e82e2af9ab1f to your computer and use it in GitHub Desktop.
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.
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