Skip to content

Instantly share code, notes, and snippets.

@sumew
Created February 25, 2016 17:10
Show Gist options
  • Save sumew/1cac2ec436880ebb06d2 to your computer and use it in GitHub Desktop.
Save sumew/1cac2ec436880ebb06d2 to your computer and use it in GitHub Desktop.
Convert an RX Observable to a Scala Promise
/**
* 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());
};
}
@sumew
Copy link
Author

sumew commented Feb 25, 2016

We used this to convert method calls that return an Rx Observable into Scala Future/Promises

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment