Search code examples
javascriptyoutube-apiyoutube-data-api

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.


Solution

  • The problem seems to that when using the gapi that it returns a promise that I want to return. Promised are not evaluated immediately which means that it continues to execute the following functions, returning a value that has no response yet.

    I solved the issue by using the async/await functionality to work with promises, which seemed to be more understandable and elegant in my eyes. I also changed some variables to constants as they did not need to be non-constants. My result looks like the following:

    await function execute() {
      let response = await gapi.client.youtube.commentThreads.list({
        "part": [
          "snippet"
        ],
        "fields":[
          "items/snippet/topLevelComment/snippet(authorDisplayName,authorProfileImageUrl,publishedAt,textDisplay)"
        ],
        "videoId": "XXXXX"
      });
      
      return response.result;
    }
    
    async function main(){
      let response = await execute();
      console.log(response);
    }
    

    The reason why we also have to use async and await in the main() function is that by definition async functions always return promises.

    To learn more about async/await I found this article whose explanation I liked: https://javascript.info/async-await

    They also have an article about promises: https://javascript.info/promise-basics