Update CompletableFutureUtils.java
This commit is contained in:
parent
c1a8a23961
commit
8813ef3e88
@ -28,6 +28,28 @@ import org.warp.commonutils.type.ScoredValue;
|
||||
|
||||
public class CompletableFutureUtils {
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture asynchronously
|
||||
*/
|
||||
public static <T> CompletableFuture<T> getCompletableFutureAsync(Supplier<CompletableFuture<T>> completableFutureSupplier, Executor executor) {
|
||||
CompletableFuture<T> cf = new CompletableFuture<>();
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
var cf2 = completableFutureSupplier.get();
|
||||
cf2.whenComplete((result, error) -> {
|
||||
if (error == null) {
|
||||
cf.complete(result);
|
||||
} else {
|
||||
cf.completeExceptionally(error);
|
||||
}
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
cf.completeExceptionally(ex);
|
||||
}
|
||||
});
|
||||
return cf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely get a CompletableFuture or a FailedFuture
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user