0

I know how to check if a video exists within a single .get, but I have no clue how to place that within another function and do stuff with it based on an if statement, which I really want to do for this handlebars approach I'm working on. Any clue how to make this work? I'm open to a different solution, but I really just want to tweak this to make it work.

function vidExists(vidId){
    var check = $.get("https://www.googleapis.com/youtube/v3/videos?id=" + vidId + "&key=[key]&part=snippet", function (data) {
        return data.pageInfo.totalResults;
    });

    if (check > 0) {
        return true
    } else {
        return false
    }
}
Quin
  • 441
  • 5
  • 12

1 Answers1

0

Unfortuantely, the example below won't work due to usage limit; however, this is the typical way of applying callbacks to functions

function vidExists(vidId, callback){
 $.get("https://www.googleapis.com/youtube/v3/videos?id=" + vidId + "&key=[key]&part=snippet", function() {
      
        if(typeof callback == 'function') {
            callback.apply(this, arguments); // pass all arguments to callback fucntion allowing access to the XHR object if needed 
        }
    });

}

vidExists('60ItHLz5WEA', function(data){ 
   console.log(data); 
   console.log(data.pageInfo.totalResults);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Adam Azad
  • 11,171
  • 5
  • 29
  • 70