Your getData
function doesn't return a Promise, so (a)waiting
for it doesn't do much. googleData
is therefore still undefined the moment you try to log it to console.
Try , for example, wrapping your getData
code inside a Promise, like this :
const getData = () => {
return new Promise((resolve, reject) => {
googleTrends
.interestOverTime({ keyword: `Test` })
.then(function (results) {
return resolve(results);
})
.catch(function (err) {
return reject(err);
});
});
}
Also I would suggest adding a try ... catch
block to the first async function, like this
const returnBool = async () => {
try {
const googleData = await getData();
console.log(googleData);
} catch (ex) {
}
};
Actually, depending on what you are actually trying to do, you could save some lines there and get rid of the additional function like this :
const returnBool = async () => {
try {
const googleData = await googleTrends.interestOverTime({ keyword: 'Test' });
return googleData ? true : false;
} catch (ex) {
/* error handling */
return false
}
}
And if I may, let me suggest one more thing. returnBool
is not really a good name for a function. Try something more descriptive, like
const keywordHasData = async (keyword) => {
// ... await googleTrends.interestOverTime({ keyword });
}