-2

I am trying to implement an answer given in the following question: How do I return the response from an asynchronous call?

Scroll down to the answer by Hemant Bavle (currently 62 votes). This is exactly what I am trying to implement, but still no luck. Here is my attempt (I've omitted the ajaxSetup() and fail() for brevity):

function isGoodPIN(pin) {
    var result;
    var cURL = "server/checkPIN?pin=" + pin;

    function setResult(ajaxResult) {
        result = ajaxResult; // <--------- true here...
    }

    var ajaxResponse = $.get(cURL, function (data) {
        // data is "OK" here...
        setResult(data == "OK" ? true : false);
    });

    return result; //<--------- undefined here
}

Is this a scope problem because result in setResult() is local to setResult() and not visible outside of it? If so, what is the solution to this problem?

Community
  • 1
  • 1
KWallace
  • 1,570
  • 1
  • 15
  • 25
  • Why are you still using `return` when you've defined the callback? The whole point of the answers to that question are that you cannot use a return statement when providing data from an AJAX request. *Don't use return*. All code reliant on the result of the AJAX request *has* to be placed within the callback function, in your case `setResult()`. – Rory McCrossan Mar 17 '16 at 15:13
  • Yeah, Rory. I am being taught that lesson the hard way. Was hoping to avoid doing the work in the callback. Thanks – KWallace Mar 17 '16 at 15:17
  • I'm trying to make a function that will return true/false if a given PIN is valid or not. It requires an ajax call to do it, but getting that damn value back from the ajax call to the calling function is really kicking my arse. ;-) – KWallace Mar 17 '16 at 15:18
  • You're still trying to do something that is impossible. – Kevin B Mar 17 '16 at 15:19
  • I added an answer for you to hopefully make it a bit clearer. – Rory McCrossan Mar 17 '16 at 15:19
  • It's still an async prob, I think, because when I trace/step it, the return at the bottom fires before the ajax get() fires. – KWallace Mar 17 '16 at 15:19

1 Answers1

0

You still seem to be missing the point of AJAX; that being that it's asynchronous. This means that you cannot return anything from a parent function as the return statement will execute long before any data is returned from the request.

You need to change the logic you use in your function to incorporate callback functions which are executed based on the result of your AJAX. Try this:

function isGoodPIN(pin, validCallback, failCallback) {
    $.get('server/checkPIN', { pin: pin }, function (data) {
        if (data == "OK") { // note you should really use a boolean here
            validCallback();
        } else { 
            failCallback();
        }
    })
}

isGoodPin('1234', function() {
    console.log('valid pin');
}, function() {
    console.log('invalid pin');
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Now THAT is a worthy idea. Different callbacks for different results. THAT I can do. Thanks. Will mark this as answer when able. SO puts delay on it as I'm sure you know. – KWallace Mar 17 '16 at 15:21
  • Thanks, @Rory, You put me on the right track. I'm fairly new to async, and it bites me in the arse when I don't take into account what async means, AND what the implications are. There's no way to make an ajax call and then LATER make a decision based on that call. Ultimately, any work that is going to conditionally be executed (or not) needs to be done in the success() or fail() callbacks. Once I rearranged it to do that, everything works just fine. Thanks for pointing me in the right direction. – KWallace Mar 17 '16 at 19:06
  • Coincidentally, in the question I quoted, is Hemant Bavle's solution actually NOT a workable solution? He seems to think that would work and even suggested that he's done it. – KWallace Mar 17 '16 at 19:07