Last active
December 11, 2015 23:48
-
-
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).
This file contains hidden or 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
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