Created
August 30, 2017 15:40
-
-
Save miguelortegarodriguez/ce02815b21457ef1ede8d4280940ed68 to your computer and use it in GitHub Desktop.
Scala Future to spring's DeferredResult
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 org.springframework.web.context.request.async.DeferredResult | |
import scala.concurrent.Future | |
import scala.util.{Failure, Success} | |
import scala.concurrent.ExecutionContext.Implicits.global | |
object FutureOps { | |
implicit class DeferredResultFromFuture[T](future: Future[T]) { | |
def toDeferredResult: DeferredResult[T] = { | |
val df = new DeferredResult[T] | |
future.onComplete { | |
case Success(successResult) => | |
df.setResult(successResult) | |
case Failure(error) => | |
df.setErrorResult(error) | |
} | |
df | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment