For a side project I'm working on, and for the sake of learning some more JS, I was playing with the YouTube API examples, trying to see how they work.
In Google's own documentation for getting playlists for a given YT channel we have the following asynchronous code:
var channelPlalists = null;
function getChannelPlayLists() {
return gapi.client.youtube.playlists.list({
"part": [
"id"
],
"channelId": "UCe3hdN-kzmG97aR3I3xbyJQ",
"maxResults": 50
})
.then(function(response) {
channelPlaylists = response;
},
function(err) { console.error("Execute error", err); });
}
In my case I would like to understand how I can turn this into a synchronous request, meaning that I would like the thread to be blocked until the response hasn't arrived.
How can I do that?
I saw that you can decompose the function above in these two parts:
var request = gapi.client.youtube.playlists.list({
"part": [
"id"
],
"channelId": "UCe3hdN-kzmG97aR3I3xbyJQ",
"maxResults": 50
})
request.execute(function(response) {
console.log(response);
}
however this does not help because now it's the execute() method to work in a non-blocking way.
I am also a bit doubtful that what I'm trying to do is even possible at all, as YouTube API could have been written in a non-blocking way by design, so that could be the only way to use it... but this is just my free speculation.
In the end I know this is the opposite of what people usually ask (how to make your code non-blocking) but sometimes this is how I learn stuff... :)