How do I make function out of this?
         //check if station is alive
         $.ajax({
           url: "lib/grab.php",
           data: "check_live=1&stream_url="+valueSelected,
           type: "GET",
               success: function (resp) {
                 if (resp == 1) {
                   play_this(valueSelected);
                 } else {
                   //
                 }
               },
               error: function (e) {
                 console.dir(e);
               }
         });
I thought I could do something like this:
function is_alive(valueSelected) {
result = false;
             //check if station is alive
             $.ajax({
               url: "lib/grab.php",
               data: "check_live=1&stream_url="+valueSelected,
               type: "GET",
                   success: function (resp) {
                     if (resp == 1) {
                       result = true;
                     } else {
                       //
                     }
                   },
                   error: function (e) {
                     console.dir(e);
                   }
             });
return result;
}
But obviously due to asynchronous nature of ajax call, result always returns false.
What is the trick of dealing with this situation?
Seems to work:
        //check if station is alive
        function is_alive(url) {
          //
          var result = false;
          //
          return $.ajax({
            url: "lib/grab.php",
            data: "check_live=1&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();
          });
        }
And call it like this:
    //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);
            //
          }
      });
    });
 
    