Results 1 to 2 of 2

Thread: Callbacks

  1. #1
    ImplicitCharm is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default Callbacks

    Hi All,

    I'm currently writing client-side code for a web-app in Java (using GWT, which converts it into JS), but I think my question is more a Java question than a GWT one.

    The code currently looks, roughly, like this:

    Java Code:
    public class MyProgram {
    
    [INDENT]String myVariable;
    MyServiceAsync service = GWT.create(MyService.class);
    ...
    
    [INDENT]public String myMethod() {
    [INDENT]service.getSomething(new AsyncCallback<String>() {
    [INDENT]public void onFailure(Throwable caught) {
    [INDENT]myVariable = "fail";[/INDENT]
    }
    
    public void onFailure(Throwable caught) {
    [INDENT]myVariable = "success";[/INDENT]
    }[/INDENT]
    }[/INDENT]return myVariable + "xyz";
    }[/INDENT]}[/INDENT]
    }
    But of course, myVariable remains uninitialized, and myMethod() returns only "xyz". If I'm looking to change my global variable, myVariable, how would I go about doing that?

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Well apart from the fact that it could never compile because there are two identical onFailure(..) methods in the AsyncCallback class, in principle the code should work, although given that the AsyncCallback methods are called asynchronously, the 'myVariable' String will be updated to 'success' or 'failure' at some indeterminate time after myMethod returns. That's what asynchronous means.

    If you want help with runtime problems in code you post, it helps to post code that will compile...

Similar Threads

  1. Replies: 0
    Last Post: 03-31-2008, 11:18 AM
  2. Replies: 0
    Last Post: 03-31-2008, 11:16 AM
  3. Replies: 0
    Last Post: 09-28-2007, 01:50 PM
  4. Replies: 0
    Last Post: 09-28-2007, 01:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •