title: Ajax global variables or synchronous AJAX
body: <p>I want to get data from another page using AJAX. I also want to wrap this AJAX call into my "user defined function".</p>
  
  <p>But I can not write like this:</p>
  
  <pre><code> function func(){
        var tmp;
        $.ajax({
            url: 'url',
            type: "POST",
            dataType: "json",
  
            success: function (data) {
              tmp=data;
            }
       });
  
       return tmp;
  }
  </code></pre>
  
  <p>because AJAX is asynchronous and this code returns - "undefined".</p>
  
  <p>When AJAX async param set to <strong>false</strong></p>
  
  <pre><code>var tmp=$.ajax({...});
  </code></pre>
  
  <p>possible do the trick.</p>
  
  <p>I also can create some global variables and write like this:</p>
  
  <pre><code>function setMyVariable(){
          $.ajax({
            ...
            success: function (data) {
              myGlobalVariable=data;
            }
          });
  }
  </code></pre>
  
  <p>The question is -  <strong>Is it good practice to use global variables in this case?</strong>
  <br>Or it is completely wrong and I need search something else</p>