0
var wrapper = function() {

   var condition; //boolean  

   $.get('url', function(data) {

      console.log(data); //url
      condition = true/false;

   });

  return condition; 
}

when I do wrapper() it returns always undefined because of async nature of $.get. How to I solve this issue?

RuntimeException
  • 1,135
  • 2
  • 11
  • 25
  • Solve it by not returning anything, and putting all logic in to the callback function, either directly or by calling some processing functionality. – Rory McCrossan Sep 19 '13 at 11:00

2 Answers2

2

Why not rather do something like this and use callbacks

var wrapper = function(callback) {

    var condition; //boolean  

    $.get('url', function(data) {

        console.log(data); //url
        condition = true/false;
        callback(condition);
    });

}

wrapper(function(condition){
    //do something with the returned condition
});

If you give more details I can expand this to help you further

Armand
  • 9,847
  • 9
  • 42
  • 75
2

When using asynchronous operations you do not return anything. Instead send a callback function as argument to wrapper function.

var wrapper = function(callback) {
   var condition; //boolean  
   $.get('url', function(data) {
      condition = true/false;
      callback(condition);
   });
}

wrapper(function(condition) {
   // check condition
});
letiagoalves
  • 11,224
  • 4
  • 40
  • 66