title: Can I return data from an anonymous callback function in jQuery?
body: <p>I'm sure this is simple, but I'm tripping up over a scope issue in Javascript. I'm doing the following:</p>
  <pre><code>$.get(url, function (data){console.log(data);});
  </code></pre>
  <p>This part works fine - I see the string that I want appear in the console. But what I actually want is to take that data variable and place it in a string. Something like this (doesn't work):</p>
  <pre><code>string = $.get(url, function (data){return data;});
  </code></pre>
  <p>This gives <code>string</code> a value of <code>[object XMLHttpRequest]</code>.</p>
  <p>What am I missing?</p>
  <h2>Oops</h2>
  <p>Yes, I'm missing the fact that Ajax is, as the name says, asynchronous. Thanks for giving me the slap on the forehead I needed, everyone.</p>
  <h2>Afterword</h2>
  <p>The reason I wasn't simply working with the results inside the callback function itself is that I actually need to do multiple AJAX requests every few seconds, gather up the data, and append them all to the page when they're all done.</p>
  <p>I've now got that working with a little closure function - the requests are done in a loop, and the data is passed to the closure. When the timer goes off, I call the closure function with no arguments, which tells it &quot;append your previous batch of data to the page (the requests are surely complete by now), clear your cache, and prepare to start receiving new data from the AJAX callbacks in my loop.&quot;</p>
  <p>Which (hopefully) shows that I'm not an idiot after all. :)</p>