I'm using asynchronous functions with Promise in order to make use of Google Sheets API. Everything is working fine except for one thing: I'm not able to access the value returned by the API outside of 'then'. Inside then, 'content' is printing the right value. Here is the relevant part of the code:
var val = selectPayloadContent(serviceName)
.then(content => console.log("Outputting content: ", content))
.catch(error => console.log(error));
console.log("Outputting content outside: ", val);
val.then(function(result) {
console.log("Val: ", result);
});
I realize this is a common issue and I've gone through a lot of questions and different articles. However, I can't seem to resolve this problem. Please note:
- 'val' in the console log is showing Promise {pending}.
PromiseState is fulfilled and PromiseResult is undefined. - 'result' in the console log is showing undefined
Any kind of help will be really appreciated. Thanks.