'Handle Google API Response

I want to get the comments from a YouTube video and do some further work with them. I started with the implementation example from the docs for "CommentThreads: list" and continued from there on.

Using a main function I want to call the execute function and use the response it should return. It should look kinda like the following:

function execute() {
  return gapi.client.youtube.commentThreads.list({
    "part": [
      "snippet"
    ],
    "fields":[
      "items/snippet/topLevelComment/snippet(authorDisplayName,authorProfileImageUrl,publishedAt,textDisplay)"
    ],
    "videoId": "XXXXX"
  }).then(function(response) {
              console.log(response.result);
            },
            function(err) { 
              console.error("Execute error", err); 
            });
}

function main(){
  let response = execute();
  console.log(response);
}

And the output in the console looks like this:

{
    "Ca": 2,
    "yb": null,
    "Yj": null,
    "Sn": null,
    "KB": false,
    "xw": false
}

Another way I tried to solve this is to not return gapi.client.youtube.commentThreads.list but the response in the successful promise function like so:

function execute() {
  gapi.client.youtube.commentThreads.list({
    "part": [
      "snippet"
    ],
    "fields":[
      "items/snippet/topLevelComment/snippet(authorDisplayName,authorProfileImageUrl,publishedAt,textDisplay)"
    ],
    "videoId": "XXXXX"
  }).then(function(response) {
              console.log(response.result);
              return reponse.result;
            },
            function(err) { 
              console.error("Execute error", err); 
            });
}

But here I only get an undefined.

I am just learning JavaScript and the use of the API.

EDIT: I should also add that the console.log(response.result); in the execute function does print the information I want. But as soon as I return it and want to use it in the main() function it changes.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source