How to return a value in a JS asynchronous callback function - gapi
Wrap the load in a Promise so that you can await it like the rest of your code.
try {
await new Promise((resolve,reject) => {
gapi.load('client:auth2', resolve);
});
await authenticate();
await loadYoutubeApi();
} catch (error) {
throw Error(`Error initializing gapi client: ${error}`);
}
//is Initialized
You can wrap gapi.load
part in a promise like this:
const initialize = async () => {
const { gapi } = window;
await new Promise((resolve, reject) => {
gapi.load('client:auth2', async () => {
try {
await authenticate();
await loadYoutubeApi();
resolve();
} catch (error) {
throw Error(`Error initializing gapi client: ${error}`);
}
});
});
return true;
};
initialize(); // returns 'true' when done.