0

I want to catch the returned value of eAC_callback if it was false or true when eAC calls it during onreadystatechange. Can I do that? How?

function eAC(emailData) {
    if (window.XMLHttpRequest) {
        httpRequest = new XMLHttpRequest();
    }

    if (!httpRequest) {
        return false;
    }

    var fd = new FormData();
    fd.append("email", emailData);

    httpRequest.onreadystatechange = eAC_callback;
    httpRequest.open('POST', "http://example.com/p_PEC.php");
    httpRequest.send(fd);
}

eAC_callback returns true or false when readyState is 4 and status is 200

function eAC_callback() {
    if (httpRequest.readyState === 4) {
        if (httpRequest.status === 200) {
            var response = JSON.parse(httpRequest.responseText);
            if(response.error === 0){
                return true;
            } else {
                if(response.error === 1){
                    return false;
                }
                if(response.error === 2){
                    return false;
                }
            }       
        } else {
            return false;
        }
    }
};
Jo E.
  • 7,822
  • 14
  • 58
  • 94
  • 2
    Welcome to the wonderful world of **async**! You can't do that. – SLaks Jan 19 '14 at 14:36
  • make your ajax call sync to do that but sync ajax will frezee your browser except firefox – semirturgay Jan 19 '14 at 14:40
  • 1
    you don't use the return keyword in a callback except as a void early exit, returning a value makes no sense at all because it cannot "go anywhere"... the fix is actually simple though, just do whatever you want to do after calling the ajax inside the callback instead of outside it. – dandavis Jan 19 '14 at 14:44
  • @dandavis thats the actual code right now. doing the stuff inside the callback. but I was wondering if I could get the result from the function with the ajax so I could do this `if(!eAC(emailData))`. Seeing the comments I guess I can't. too bad. – Jo E. Jan 19 '14 at 14:50
  • it's not too bad, you just put the code in your if statement into the callback. if use a reusable ajax function, it's not a lot of ugly boilerplate like you have now, it's just wrapping a single anon function around the code in question. thanks to closure, that wrapped code can fit into an existing app in the exact same place as your theoretical if statement with no refactoring or interface freeze. it's the 21st century way to code simple and efficient async operations. – dandavis Jan 19 '14 at 15:03

1 Answers1

1

I want to catch the returned value of eAC_callback

Then you will have to wrap it in another function:

httpRequest.onreadystatechange = function() {
    if (eAC_callback()) {
        …
    } else {
        …
    }
};

However, you will never be able to return any results computed in the asynchronous callback to the eAC function that installed the callback (long ago). See also How do I return the response from an asynchronous call? - supply a callback or return a promise.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375