I want to await the function call which is passed in the parameter from the in-built google API call and return the results after it parameter function is executed. But I'm not able to get on with it.
async function list(auth, userId) {
const gmail = google.gmail({ version: 'v1', auth });
const result;
await gmail.users.messages.list({
userId: userId,
}, async (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
console.log(res.data);
result = await res.data; // I want to await this part
});
return result;
}
...
const returnedResult = await list(auth, id);
console.log(returnedResult);
What happens in the above code is that 'result' and 'returnResult' remain undefined and are output first and then res.data is logged into the console. Any help would be appreciated. Thanks in advance
Edit:
Though the workaround is there to await such a call as answered below. But It still doesn't answer the internal mechanism through which it attains two forms while making API requests i.e. How
gmail.users.messages.list({...}, (err, res){...});
can be internally converted to
let result = await gmail.users.messages.list({});
or vice versa. The one possible explanation seems to be method overloading. But even this concept doesn't explain it since both the calls have exactly the same functionality. And for the first implementation since it is also a call involving promises it should still have some mechanism to await the request.
With regards to the following:
Awaiting on a non-promise value wouldn't help solving your problem. Assuming
gmail.users.messages.list(...)returns a promise, instead of setting the result in theonFulfilledfunction, try this: