Reputation: 519
In my GWT-Platform application I have implemented method in which one step is to fetch data from server and next step is dependent on it. I want to prevent my method for further execution of the code until the Async call completes.
Should be something simple but I am not finding a way.
Upvotes: 4
Views: 9364
Reputation: 1528
I had to change the original flow, basically move it and chain to the Async call's onSuccess().
Originally, the flow is
Now In a new scenaro, the #2 validate is changed to require a Async call back to the back end. So #3 has to be moved to chain to the callback on the validate method. ' Code snippets below
public void onValidationDataReady(List<Long> existingTests) throws ValidationException {
if (!existingTests.isEmpty()) {
throw new ValidationException("The entered name has already been used.");
}
//only after the validation do we proceed with the original OK click
proceedOkClick(testNameField.getValue());
}
public void proceedOkClick(String data) {
// proceed only after the async call
if (callback != null) {
callback.onDialogResult(true, data);
}
clearDialog();
}
public boolean validateInputs() throws ValidationException {
//async call to get data from back end.
//pass in a Callback
testNameValidator.validate(testNameField.getValue(), new DataReadyCallback<List<Long>>() {
@Override
public void onDataReady(List<Long> existingTests) {
onValidationDataReady(existingTests);
}
});
return true;
}
//The call back interface.
public interface DataReadyCallback<T> {
void onDataReady(T data);
}
Upvotes: 0
Reputation: 17489
I think you are missing the point about the web being asynchronous.
It is not considered good practice (it is rather an anti-pattern) to block the execution of your client side code until the async call is finished.
So instead of blocking the execution until your async code is finished do following:
Upvotes: 6
Reputation: 12212
Why using a Timer
?
final Button sendButton = new Button("Send");
sendButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
service.sendInfo("Send First Step Info",
new AsyncCallback<String>() {
@Override
public void onSuccess(String result) {
Window.alert("Success Call");
nextStep(result);
}
@Override
public void onFailure(Throwable caught) {
Window.alert("Failure Call");
}
});
}
});
private void nextStep(String data) {
}
Upvotes: 1
Reputation: 410
I'm not GWT guru, but i'm know how do it in simple way. I would be very grateful if someone tell how to do it the right way, because I was interested in it too. You can just make method which will contain required code and calls it onSuccess or do something like this:
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
public class Aaa implements EntryPoint {
private final MyServiceAsync service = GWT
.create(MyService.class);
private Timer t;
public void onModuleLoad() {
t = new Timer() {
@Override
public void run() {
// Make something or call function
Window.alert("Next Step");
}
};
final Button sendButton = new Button("Send");
sendButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
service.sendInfo("Send First Step Info",
new AsyncCallback<String>() {
@Override
public void onSuccess(String result) {
Window.alert("Success Call");
// Call Next step code or function
t.schedule(1);
}
@Override
public void onFailure(Throwable caught) {
Window.alert("Failure Call");
}
});
}
});
RootPanel.get().add(sendButton);
}
}
Upvotes: 2