Is it ever possible for an asynchronous service method's callback to get called without yielding to the browser's event loop?

105 views
Skip to first unread message

Tad

unread,
Jun 17, 2011, 12:29:41 PM6/17/11
to Google Web Toolkit

Is the AsyncCallback framework implemented in such a way that when a
RemoteService method is called, is it a GUARANTEE that the browser's
event loop will execute at least one cycle before its callback (i.e.
onSuccess or onFailure) gets called?

For example:

...some code...
myRemoteService.myMethod( myParameter, new AsyncCallback<String>()
{
public void onSuccess(String result)
{
....
}
});
...some "long running" code...

In the above example, if the result of myMethod happens to return
before the "some long running code" finishes, will the browser's event
loop always receive at least one cycle before onSuccess() is called?
Or is it ever possible that onSuccess() could get called without
yielding to the browser's event loop?

Another way to ask this question would be: Is there ever a scenario
where it is necessary to do the following (or put the scheduleDeferred
call inside myMethod's onSuccess callback)?

Schedule.get().scheduleDeferred( new ScheduledCommand()
{
myRemoteService.myMethod( myParameter, new AsyncCallback<String>()
{
public void onSuccess(String result)
{
....
}
});
});

-Tad

Thomas Broyer

unread,
Jun 17, 2011, 9:28:04 PM6/17/11
to google-we...@googlegroups.com
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.
Does that answer your question?

More details of how browsers behave (or should/will behave in the near future) can be found in the HTML5 spec http://www.whatwg.org/html5 or http://www.w3.org/TR/html5

Derek

unread,
Jun 18, 2011, 10:15:05 AM6/18/11
to Google Web Toolkit
The only time I encounter what you describe is when I mock the server
side code on the client side. In other words, in your code example, I
might have myRemoteService actually be an instance of some
MyRemoteServiceAsyncMock, that returns dummy data while I wait for
someone else to write the server side impl (or until I do it myself).
In that case the async call is actually sync and so I might need a
deferred command to make sure that code outside the async call is run
before the call itself. Otherwise you should be able to count on
things happening in the expected order.

Tad

unread,
Jun 24, 2011, 12:54:38 AM6/24/11
to Google Web Toolkit
Derek,

Interesting; that makes complete sense with the mock remote service
call.

So under usual conditions would you say "the expected order" is:

1 - my client-side code executes and makes an async remove service
call
2 - my client-side code continues to execute "long-running" code and
the async call's result comes back while this is still executing
3 - browser's event loop gets to execute once.
4 - my async-callback defined in step #1 is called.

I want to be certain that step #3 IS called under those conditions in
step #2.

-Tad

Tad

unread,
Jun 24, 2011, 12:57:13 AM6/24/11
to Google Web Toolkit
Thomas,

Yes, I believe that is the answer I was looking for.

-Tad

Karthik Reddy

unread,
Aug 12, 2011, 5:58:49 PM8/12/11
to google-we...@googlegroups.com
See the example under "Non-Blocking / Asynchronous: " and it gives a clear example addressing Tad's question:

Reply all
Reply to author
Forward
0 new messages