Instantly share code, notes, and snippets.

Embed
What would you like to do?
CompletableFuture to F.Promise Adapter
The following adapter is inspired by mattiwintou adapter from Rx Observable to F.Promise
https://gist.github.com/matiwinnetou/7b69253acaad996747c4
public F.Promise<Result> rankedPairs() {
CompletableFuture cf = ...
return RxFuture.toPromise(cf).map(Controller::ok);
}
package com.dimascio.play;
import play.libs.F;
import java.util.concurrent.CompletableFuture;
public class RxFuture<T> {
private final CompletableFuture<T> cf;
public RxFuture(final CompletableFuture<T> cf) {
this.cf = cf;
}
public static <E> F.Promise<E> toPromise(final CompletableFuture<E> obs) {
return new RxFuture(obs).adopt();
}
public F.Promise<T> adopt() {
F.RedeemablePromise<T> rPromise = F.RedeemablePromise.empty();
cf.whenCompleteAsync((res, err) -> {
if (err != null) {
rPromise.failure(err);
} else {
rPromise.success(res);
}
});
return rPromise;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment