0

I have this situation where I have to check something via AJAX and then return the result. Or simplified:

function isValid() {
    $.ajax(URL, data, function(response) {
        isValid = response;
    });
    return isValid;
}

but still can't make it.

I can access the reponse, but I can't make isValid return AFTER I get the reponse. For example the solution I meet everywhere:

function isValid() {
    function checkOnServer(callback) {
        $.ajax(URL, data, function(response) {
            callback(response);
        });
    }
    checkOnServer(function(response) {
        alert(response);
    });
}

I met this all over Stackoverflow, BUT the problem is:

  1. I don't want to alert this.
  2. I want to RETURN it from isValid().

===========================

EDIT: I forgot to mention that if you/me/we try to simply "return" from the "checkOnServer" - it will just return it to the success callback of the AJAX. But the goal here is to make "isValid" return the result ... :)

Kevin B
  • 94,570
  • 16
  • 163
  • 180
pesho hristov
  • 1,946
  • 1
  • 25
  • 43
  • 3
    You can not return from an asynchronous method. You are trying to eat the delivery pizza before it is delivered to your house. You need to use callbacks or promises. You need to change your logic. – epascarello Feb 19 '16 at 15:00
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Hatchet Feb 19 '16 at 15:21

1 Answers1

0

your code is sensible ,but you use your callback function in isValid() so its doesn't alert !,also I've a little bit change your ajax function below

function isValid() { 
  checkOnServer(function(response) {
    alert(response);
  });
}
function checkOnServer(callback) {
    $.ajax({
         url : URL,
         data : data,
         success : function(response) {
           callback(response);
         }
     });
}

Edit

Variable is can't return from asynchronous method ?If we call function with ajax ,call function is run first immediately before ajax is response,so it will return undefined only with above method.

There is two method I've know to return from asynchronous function

Call after ajax function is done

 function isValid() {
  var data; 
  checkOnServer(function(response) {
    data = response;
  });
  return data;
}
function checkOnServer(callback) {
    $.ajax({
         url : URL,
         data : data,
         async : false             
     }).done(function(response){
        callback(response);
        });
}
console.log(isValid());

Assign variable with responseText

 function isValid() { 
 var data = checkOnServer();
 return data;
}
function checkOnServer() {
    var data = $.ajax({
                 url : URL,
                 data : data,
                 async : false //must set async to false
              });
    return data.responseText;
}
console.log(isValid());
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
  • And in your commend - how can you make isValid() return the response ? – pesho hristov Feb 22 '16 at 07:21
  • callback is can't return variable ,so we make all code within that call back function.What would you like to do with that return response? ,do it that in isValid() – Jack jdeoel Feb 22 '16 at 08:24
  • The "isValid" is legacy, and I must use it. The only thing I can do is just adding stuff there and modify the "return" statement. (But it should always return "true" or "false" in order to perform validation.) – pesho hristov Feb 22 '16 at 12:03