views:

189

answers:

4

Hi there.

Im trying to return a callback value after the data is loaded, Im probably looking at this all wrong.

    var loadFromDatabase = function(callback){
  if (!obj.data.myarray) {
      // the problem is here, the jquery obj returns the ajax request obj
    return $.getJSON('http://validjson',function(data){
          obj.data.myarray = data.myarray;
          return callback();
        }); 
      } else {
    return callback();
  }
};
var findObjInArray = function(){
      // need i to be returned, but isnt working in the if statement, but works in the else statement
  return loadFromDatabase(function(){
    var l = obj.data.myarray.length;
    for (var i = 0; i < 50;i++) {
      if (i === 30) {
      return i;
        }
  }
    });
  };
  var myval = findObjInArray();
  console.log(myval);
+2  A: 

Should be just:

return callback;

Doing return callback() executes the callback function and returns its return value.

reko_t
Assuming that is what you wanted to do, I wasn't quite clear on that.
reko_t
That doesnt make sense to me. `function a(callback){return callback}` returns the value you passed to it..
Ghommey
Yeah, that's true, I didn't even notice that he was passing it as parameter to the outer function.
reko_t
A: 

you have to use one more callback function:

var loadFromDatabase = function(callback){
        if (!obj.data.myarray) {
                    // the problem is here, the jquery obj returns the ajax request obj
                return $.getJSON('http://validjson',function(data){
                  return callback(data.myarray);
                    });

        } else {
                return callback();
        }

};


var findObjInArray = function( callback ){
      // need i to be returned, but isnt working in the if statement, but works in the else statement
  return loadFromDatabase(function( myarray ){


        var l = myarray.length;
        for (var i = 0; i < 50;i++) {
          if (i === 30) {
                 return callback ( i );
          }
        }
  });
};


findObjInArray(
   function ( myval )
   {
      console.log(myval);
   }
);
Ghommey
Thank you, this works perfectly.
Sveisvei
A: 

The problem is that the Ajax callback is called asynchronously. This means that the loadFromDatabase function returns first and then the callback is called. Therefore it is impossible to return from something from the callback.

You could use the async Ajax option to make the call asynchronous, but I don't recommend this. You need to revisit your architecture by taking into consideration the asynchronous nature of Ajax.

kgiannakakis
+2  A: 

Ajax is asynchronous (hence the name). You can't return anything from it.

The getJSON method causes an HTTP request to be sent. The function you pass is called on the event when that request is answered. It has no link to the calling function.

You have to write that function to do what you want to do with the data itself. You can't pass the data back and handle it in the calling function.

David Dorward