Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How far will GWT AsyncCallback execute while waiting for the response?

If I were to call from a function (all written in Java):

public int hello() {
int a = 1;
executeCallback();
// C: Question lies in this range
return a;
}

public void executeCallback() {
// A: random code to execute before asynccallback
   randomClass.randomMethod(int a, int b, AsyncCallback<ReturnType>() {
      onSuccess();
      onFailure();
   });
// B: random code to execute after asynccallback
}

I understand that the stuff in comment A will execute, and concurrently the non synchronous randomMethod will execute and the comment in B will execute.

I was wondering, though, while randomMethod is executing (if it takes sufficiently long), will the function return to its caller (in this case the method 'hello') and start executing the code in comment C? Or will executeCallback wait for randomMethod to finish before it returns?

And if it's the former, assume I need the information that randomMethod touches to be touched before I can continue on to comment C, how can I make it 'wait' to ensure that this will be the case?

like image 670
RankWeis Avatar asked Feb 17 '11 06:02

RankWeis


2 Answers

When asynchronous method is called, program does not wait that method , that is why they are called asynchronous. There is no way that randomMethod AsyncCallback onSuccess or OnFailure methods are executed before the code represented as B.. Because , the browser executes javascript code in a single thread, onSuccess or OnFailure methods is executed after the caller of executeCallBack method finish.

If you want code B and code C are executed after randomMethod , you should put them onSuccess method, such as ;

randomClass.randomMethod(int a, int b, AsyncCallback<ReturnType>() {
      onSuccess() {
         // B: random code to execute after asynccallback
        // C: Question lies in this range
        }
      }
      onFailure()
   });
like image 64
Gursel Koca Avatar answered Sep 27 '22 18:09

Gursel Koca


Let me explain the execution model a bit, as it's sometimes easier if you know what happens "behind the scenes".

All code execution is initiated by the browser. This happens at certain events, e.g. when the page loads, when the user has clicked something, or when an AJAX response arrives.

So what you do when writing a GWT (or other JavaScript) application is, that you register handlers, e.g. by using onModuleLoad(), or by registering a ClickHandler to a Button, or an AsyncCallback to a GWT-RPC call.

The interesting (and maybe counter-intuitive?) thing is, that when the browser calls such a handler, it is executed till it finishes (or until an error occurs). Only after that, other handlers will be executed. This also means by the way, that if the code for one handler contains an endless loop, other callbacks will never be executed - the entire browser tab will block.

So when your hello() method is executed, this is done as part of some handler (e.g. onModuleLoad or a ClickHandler). It

  • sets a = 1,
  • then sends an AJAX request (randomClass.randomMethod),
  • registers another AsyncCallback handler (remember, this is just a registration. The browser will first finish the current handler, before it can get to actually executing that AsyncCallback handler).
  • executes B
  • executes A
  • continues executing up the call hierarchy, until the handler is finished.
like image 23
Chris Lercher Avatar answered Sep 27 '22 18:09

Chris Lercher