title: how to make ajax synchronous?
body: <p>How do I make function out of this?</p>
  
  <pre><code>         //check if station is alive
           $.ajax({
             url: "lib/grab.php",
             data: "check_live=1&amp;stream_url="+valueSelected,
             type: "GET",
                 success: function (resp) {
                   if (resp == 1) {
                     play_this(valueSelected);
                   } else {
                     //
                   }
                 },
                 error: function (e) {
                   console.dir(e);
                 }
           });
  </code></pre>
  
  <p>I thought I could do something like this:</p>
  
  <pre><code>function is_alive(valueSelected) {
  result = false;
               //check if station is alive
               $.ajax({
                 url: "lib/grab.php",
                 data: "check_live=1&amp;stream_url="+valueSelected,
                 type: "GET",
                     success: function (resp) {
                       if (resp == 1) {
                         result = true;
                       } else {
                         //
                       }
                     },
                     error: function (e) {
                       console.dir(e);
                     }
               });
  return result;
  }
  </code></pre>
  
  <p>But obviously due to asynchronous nature of ajax call, result always returns false.</p>
  
  <p>What is the trick of dealing with this situation?</p>
  
  <p>Seems to work:</p>
  
  <pre><code>        //check if station is alive
          function is_alive(url) {
            //
            var result = false;
            //
            return $.ajax({
              url: "lib/grab.php",
              data: "check_live=1&amp;stream_url="+url,
              type: "GET",
                  success: function (resp) {
                    if (resp == 1) {
                      //
                      result = true;
                      //
                    }
                  },
                  error: function (e) {
                    console.dir(e);
                  }
            }).then(function() {
              return $.Deferred(function(def) {
                def.resolveWith({},[result,url]);
              }).promise();
            });
          }
  </code></pre>
  
  <p>And call it like this:</p>
  
  <pre><code>    //Change song on select, works both for fav and station lists
      $(document).on("click", ".ui-listview li a", function(){
        var valueSelected = $(this).data("station-url");
        //
        is_alive(valueSelected).done(function(result,url){
            if (result) {
              //
              play_this(valueSelected);
              //
            }
        });
      });
  </code></pre>