With GWT-RPC, an async callback looks like this:
AsyncCallback<ResultBean> callback = new AsyncCallback<ResultBean>() {
public void onSuccess(ResultBean result) {
// Code to run once callback completes
}
public void onFailure(Throwable caught) {
// Error handling code
}
};
asyncService.call(callback);
// Subsequent code block
The onSuccess()
method will get called once the results have been received from the server. The subsequent code block will get executed before the callback completes, because the single thread has to complete execution of the current event before it can process the next one in the queue. To make sure that some code executes after the callback completes, it should be called from the onSuccess()
method.
Here's a technical explanation of how this works in a single threaded environment (found here from Thomas Broyer):
GWT-RPC makes use of RequestBuilder, which is based on XMLHttpRequest.
XMLHttpRequest (XHR) uses events to communicate back with the code, so
anything happening on an XHR results in an event being pushed on the
event queue, and dequeued by the event loop.
See also the GWT documentation.