Created
February 25, 2016 17:10
-
-
Save sumew/1cac2ec436880ebb06d2 to your computer and use it in GitHub Desktop.
Convert an RX Observable to a Scala Promise
This file contains 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
/** | |
* Converts an instance of <code>Observable<T></code> into | |
* an instance of <code>F.Promise<T></code> | |
* | |
* @return A <code>Promise<T></code> containing the value | |
* emitted by the observable | |
*/ | |
public static <T> Function<Observable<T>, F.Promise<T>> observableToFuture() | |
{ | |
return obs -> { | |
scala.concurrent.Promise<T> scalaPromise = scala.concurrent.Promise$.MODULE$.<T>apply(); | |
obs.subscribe( | |
result -> scalaPromise.success(result), | |
throwable -> scalaPromise.failure(throwable) | |
); | |
return F.Promise.wrap(scalaPromise.future()); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We used this to convert method calls that return an Rx Observable into Scala Future/Promises