Question

rpc.call(mycallback);

{
//subsequent code block

}
  1. How does the single threaded async callback work?
  2. When will the callback get called?
  3. Will the subsequent code block always finish executing before the callback is allowed to run (i.e. will the callback only run once all code has finished?)?
Was it helpful?

Solution 3

#1 javascript is single-thread, but the browser is not, so the js-thread sends the xhr call to the browser to resolve it, and the browser returns the control to it inmediately.

#2 when the browser gets the response from the server, it queues the callback into the js thread, so it will be executed when js finishes whatever it could be executing now (in your case the subsequent code block)

#3 yes it will, because the single threaded execution of this block prevents the execution of any other deferred code (timeouts, callbacks) until it is finished.

OTHER TIPS

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.

At the moment there is no difference between

rpc.call(mycallback);
{
   //subsequent code block
}

or

{
   // code block before
}
rpc.call(mycallback);

However, I do not see any reason to depend on such a behavior. If you want to be sure that the code block has been executed, use the second version

As the call made Asyncronously hence you don't know when the callback returns from server.

So,

when the code Server call starts the //subsequent code block starts executing ,And when the call ends the code in onSuccess code blocks starts executing .

If onSuccess code block and subsequent code block are independent there is no difference whether calling before or after the server hit.

if both are dependent

rpc.call(new AsynchCallback { 
        onSucees(){
                   //subsequent code block
                  }
   });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top