0

Here goes my function that receives some data and has to return it or pass it down to other function, how can return data from the inner based functions? Easy question but I just never used something like this

getData: function(callback){

    $.ajax({
        url: myUrl
    }).done(function(data){
        $(document).ready(function(){ 
            (callback) ? callback(data) : return data
        });
    }); 
}
Ian
  • 50,146
  • 13
  • 101
  • 111
Leg0
  • 510
  • 9
  • 21
  • Easy question - easy answer: you can't. AJAX. – dfsq Mar 19 '13 at 16:21
  • You can't, the outer function returns first. – bfavaretto Mar 19 '13 at 16:21
  • @dfsq Um, you can make the AJAX request just a JAX request by turning off asynchronousness. http://api.jquery.com/jQuery.ajax/ – Grant Thomas Mar 19 '13 at 16:22
  • @GrantThomas You can but it's better not to tell about it to new developers. – dfsq Mar 19 '13 at 16:23
  • 1
    @GrantThomas I agree, but there's a difference between providing information and suggesting it. A little more detail would've been helpful – Ian Mar 19 '13 at 16:28
  • @GrantThomas The responsibility of the good teacher is to teach better and good practice first. Once a learner is ready he will learn everything. In the code example above we can see `callback` parameter. It's there by purpose hence turning asynchronous behaviour off is not the right move to solve the problem. – dfsq Mar 19 '13 at 16:30
  • @dfsq Speaking of teaching, see my comment above. Discouragement is not teaching. – Grant Thomas Mar 19 '13 at 16:32
  • @GrantThomas First, I said I agreed, and I still agree with your last comment. I'm all for encouragement of what's possible. I think you missed my point though. I just think it would've been a lot more helpful to explain what "complications" or "problems" might arise with using synchronous requests, and also the possible improvements. Also, I think it would've been helpful to work with the OP and explain how to restructure their code, because they currently have it set up to use a callback (asynchronousness), and it might be a little confusing to rework. – Ian Mar 19 '13 at 16:35
  • @GrantThomas Ahh no problem, I just wanted to make sure you understand what I meant :) And I do that a lot too (use "you"). – Ian Mar 19 '13 at 16:38
  • maybe i'm not understanding this but it would seem that if you want to return the data received via ajax to another function then why wouldn't you just do this in the success callback? write a function expression that takes in a param and pass ur data to it in the success callback. is this not what OP wanted? – iAmClownShoe Mar 19 '13 at 16:43
  • Wow guys, haha. So I just wanted to make less code and use this function for both getting data on one page and getting data on the other page for different purposes, which kinda involves using different function dealing with received data. Decided to do this in such way to prevent request duplications and totally forgot about "A" part of ajax. Would appreciate some help, thanks for such interest! – Leg0 Mar 19 '13 at 16:44

0 Answers0