title: How do I make jQuery wait for an Ajax call to finish before it returns?
body: <p>I have a  server side function that requires login. If the user is logged in the function will return 1 on success. If not, the function will return the login-page.</p>
  
  <p>I want to call the function using Ajax and jQuery. What I do is submit the request with an ordinary link, with a click-function applied on it. If the user is not logged in or the function fails, I want the Ajax-call to return true, so that the href triggers. </p>
  
  <p>However, when I use the following code, the function exits before the Ajax call is done.</p>
  
  <p>How can I redirect the user gracefully to the loginpage?</p>
  
  <pre><code>$(".my_link").click(
      function(){
      $.ajax({
          url: $(this).attr('href'),
          type: 'GET',
          cache: false,
          timeout: 30000,
          error: function(){
              return true;
          },
          success: function(msg){ 
              if (parseFloat(msg)){
                  return false;
              } else {
                  return true;
              }
          }
      });
  });
  </code></pre>