I notice that when I make str a global variable, instead of making it return from a function like I do in the search function below, it works as it should. However, when I try and return it, it returns undefined. How do I resolve this? I do not want to use globals as I want the search function to be atomicized.
function search(query) {
var str;
// Use the JavaScript client library to create a search.list() API call.
var request = gapi.client.youtube.search.list({part:'snippet',q:query});
request.execute(function(response) {
str = JSON.stringify(response.result);
str = JSON.parse(str);
//console.log(str);
});
return str;
}