1

edit: the two questions linked above are not like what I need. I don't have a URL instead Im using DWR to call the java object directly from javascript so they don't help me.

I want to check if a person is dead many times on a page and I wan to reuse a function so I dont have to write the ajax stuff many times. I want to reuse isDead()

The problem is it always returns false because it doesn't wait for the ajax callback.

How can I make it wait before returning true or false?

function isDead(personId){
        PeopleManager.isPersonDead(personId,{
            callback : function(result)
            {
                if(result == 1){
                    //dead
                    return true;
                }else{
                    //alive
                    return false;
                }
            },
            errorHandler : function(e){
                alert(e); 
                }
        });

        return false;
     } 
apsillers
  • 112,806
  • 17
  • 235
  • 239
124697
  • 22,097
  • 68
  • 188
  • 315
  • 1
    Don't make it wait, use callbacks. – dfsq Apr 08 '13 at 13:34
  • possible duplicate of [Wait until all jquery ajax request are done?](http://stackoverflow.com/questions/3709597/wait-until-all-jquery-ajax-request-are-done) – Ajinkya Apr 08 '13 at 13:35
  • Possible duplicate of [Return data after ajax call success](http://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success/) – apsillers Apr 08 '13 at 13:37
  • @user521180 How does DWR change the fact that you need to use an asynchronous pattern? Matt Greer's answer is exactly right. Code that needs to use a result from `isDead` should go inside of a callback function that is passed to `isDead`. – apsillers Apr 08 '13 at 13:43
  • @apsillers we know that already. i am asking if there is a way to do it so that the function waits for the callback like synchronizing threads in java. – 124697 Apr 08 '13 at 13:46
  • @user521180 [Yes, there is](http://readystate4.com/2008/09/18/making-a-synchronous-dwr-call/): you need to define a `_getData` function that performs a synchronous fetch. Synchronous Ajax calls are universally regraded as bad design, since they lock up the entire page until they complete, and asynchronous code is exactly as powerful without that substantial disadvantage. – apsillers Apr 08 '13 at 13:56

3 Answers3

3

Luckily for me there is an answer specific to DWR. It turns out you can disable the asynchronous functionality with async:false

function isDead(personId){
    DWREngine.setAsync(false);
            PeopleManager.isPersonDead(personId,{
                callback : function(result)
                {
                    async:false,
                    if(result == 1){
                        //dead
                        return true;
                    }else{
                        //alive
                        return false;
                    }
                },
                errorHandler : function(e){
                    alert(e); 
                    }
            });
    return false;
} 
124697
  • 22,097
  • 68
  • 188
  • 315
  • Is this working? I believe the true/false is still not being returned at the right level, and you will always get false. Also turning off async can mean really bad performance, so keep that in mind. – Matt Greer Apr 08 '13 at 14:49
  • @MattGreer Yes it did the job. Async is being turned on again before the return – 124697 Apr 08 '13 at 15:02
2

Generally you can't, this is the main gotcha of asynchronous programming. Technically you can make the AJAX call be synchronous, but that's a bad idea. Instead isDead() will need to accept a callback to receive its answer

function isDead(id, callback) {
    PeopleManager.isPersonDead(id, function(result) {
        callback(result === 1);
    });
}

Some details have been left out, but that's the general idea. You can also look into promises for a more modern approach to this problem

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • 1
    To clarify, use this like: `isDead(197, function(deadBool) { if(deadBool) alert("He's dead!") })`. You pass in a function as the second argument to `isDead`. The function gets run (and passed a boolean result as its first argument, `deadBool`) whenever the Ajax call completes. – apsillers Apr 08 '13 at 13:47
0

Use a promise instead.

Read the article if you are unfamiliar with promises. To get an example related to your question jump to the section "Accept Promises As Parameters"

http://www.kendoui.com/blogs/teamblog/posts/13-03-28/what-is-the-point-of-promises.aspx

N..
  • 119
  • 1
  • 2
  • 7