Skip to content

Instantly share code, notes, and snippets.

@bmc
Last active December 11, 2015 23:48
Show Gist options
  • Save bmc/4679048 to your computer and use it in GitHub Desktop.
Save bmc/4679048 to your computer and use it in GitHub Desktop.
Transitional code to enhance Play 2.1 library so that Scala Futures have the await() function that Play 2.0 Promises had (until I can refactor).
import scala.concurrent.Future
import scala.concurrent.duration._
import play.api.libs.concurrent.Execution.Implicits._
object WebServicesUtil {
import scala.language.{implicitConversions, postfixOps}
/** TRANSITIONAL: Simulate Play 2.0 await() for a Future, until code
* is refactored.
*/
class TransitionalFuture[+T](val future: Future[T]) {
class NotWaiting[+A](future: Future[A]) {
import scala.concurrent.Await
def fold[B](onError: (Throwable) => B, onSuccess: (A) => B): B = {
try {
onSuccess(Await.result(future, 30 seconds))
}
catch {
case e: Throwable => onError(e)
}
}
def get: A = Await.result(future, 30 seconds)
}
def await: NotWaiting[T] = new NotWaiting(future)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment