How do I make sure that java waits for the value from the onSucces method: https://pastebin.com/TR6ea2QZ ? (I cut out the irrelevant stuff)
public float getMoney(Player player){ Float[] money = new Float[1]; try { //SOME CODE FOR DB .getMoneyAsnyc(statement, new Callback<Float>() { asyncMySQL @Override public Float onSucces(Float money) { //This value should be returned!!! [0] = money; money} @Override public void onFailure(Throwable cause) { } }); } catch (SQLException e) { .printStackTrace(); e} return money[0]; }
I want to call out that, long term, it;s going to be a better tech investment to use blocking sql because loom, but yes, you can return a Future
public class FutureExample {
interface ExampleAsync {
void onSuccess(float money);
void onFailure(Throwable throwable);
}
private void someThing(ExampleAsync exampleAsync) {}
public Future<Float> getMoney() {
final var future = new CompletableFuture<Float>();
try {
someThing(new ExampleAsync() {
@Override
public void onSuccess(float money) {
.complete(money);
future}
@Override
public void onFailure(Throwable throwable) {
.completeExceptionally(throwable);
future}
});
} catch (Exception e) {
.completeExceptionally(e);
future}
return future;
}
}