title: Accessing a JSON object outside its callback method
body: <p>I am writing a function, which will connect to a server and retrieve a JSON object. It will process it and return a normal array with values to be loaded in some HTML tag. I know it can be done using other ways, but I am using the following code.</p>
  
  <pre><code>var names = new Array();
  function fetch (key) {
    var url = 'server/server.php?key=' + key
    $.getJSON(url , sett = function(data) {
      $.each(data, function(key, value){
        names[key] = value['name'];
      });
      console.log(names);//works and print the values
    });
    console.log(names);//doesn't work, shows and empty array
  }
  </code></pre>
  
  <p>I want to return an array which will hold the values filtered by jQuery each, so this function can work as a standard function for any big application. How it can be done?</p>