Skip to content

Instantly share code, notes, and snippets.

@plaflamme
Forked from arschles/twitterFutureToScala.scala
Last active May 24, 2016 04:26
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save plaflamme/5509134 to your computer and use it in GitHub Desktop.
Save plaflamme/5509134 to your computer and use it in GitHub Desktop.
import com.twitter.util.{Future => TwFuture}
import scala.concurrent.{Future => ScFuture, promise => ScPromise}
implicit def twFutureToScala[T](twFuture: TwFuture[T]): ScFuture[T] = {
val prom = ScPromise[T]
twFuture.onSuccess { res: T =>
prom.success(res)
}
twFuture.onFailure { t: Throwable =>
prom.failure(t)
}
prom.future
}
import com.twitter.util.{ Future => TwFuture, Try => TwTry }
import scala.util.{Try => ScTry}
import scala.concurrent.{ Future => ScFuture, promise => ScPromise }
implicit def twTryToScalaTry[T](tw : TwTry[T]) : ScTry[T] = {
ScTry(tw.get)
}
implicit def twFutureToScala[T](twFuture: TwFuture[T]): ScFuture[T] = {
val prom = ScPromise[T]
twFuture.respond { t : TwTry[T] =>
prom.complete(t)
}
prom.future
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment