Skip to content

Instantly share code, notes, and snippets.

@miguelortegarodriguez
Created August 30, 2017 15:40
Show Gist options
  • Save miguelortegarodriguez/ce02815b21457ef1ede8d4280940ed68 to your computer and use it in GitHub Desktop.
Save miguelortegarodriguez/ce02815b21457ef1ede8d4280940ed68 to your computer and use it in GitHub Desktop.
Scala Future to spring's DeferredResult
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