I am trying to assign a value to a variable at the parent function level through some nested self-invoking functions. I am new to javascript coding and am having some trouble getting the value of the variable out of the nested function. Is the value being loaded asynchronously? Or is there another fix to this?
function searchA(keyword_string) {
var video_id = '';
var q = keyword_string;
var request = gapi.client.youtube.search.list({
q: q,
part: 'snippet',
maxResults: '1',
type: 'video',
order: 'relevance',
videoEmbeddable: 'true'
});
request.execute(function(response) {
var str = JSON.stringify(response.result);
var json = response.result;
video_id = json.items[0].id.videoId;
console.log(video_id); //THIS GIVES THE CORRECT ID
});
console.log(video_id); //THIS RETURNS AN EMPTY STRING
}