I have been looking thru many excellent online examples of getting a Promise to return a value in the .then(). However, there must be something different in my example that is blocking it. I am using nativescript-vue if that makes a difference, but really I think this is a Javascript question.
I have calling vue file:
var person;
personService.retrievePerson(this.id).then(function(val) {
console.log("===> received val: " + val);
person = val;
}).catch(function(err) {
console.err(err);
let message = "Unable to find matching person for id. Please try again.";
alert(message)
.then(() => {
console.log(message);
});
});
Then in my PersonService.js:
retrievePerson(id) {
new Promise(resolve => {
httpModule.request({
url: PersonAPI,
headers: headers,
method: "GET"
}).then((response) => {
// resolve();
let status = response.statusCode;
if (status === 200) {
let content = JSON.parse(response.content);
Person = JSON.parse(response.content);
console.log("===> returned [200] PersonService.retrievePerson() response: \"" + JSON.stringify(Person) + "\"")
} else if (status === 404) {
console.log("===> returned [404] ... returning null ");
}
// resolve();
}).then(() => {
console.log("===> PersonService.retrievePerson().then() has Person : " + JSON.stringify(Person));
resolve(Person);
}).catch((err) => {
console.error("retrievePerson() caught error: " + JSON.stringify(err));
});
});
}
In my console logs I can see the ===> returned [200] PersonService.retrievePerson() person JSON output, but the calling console "===> received val" is not shown. But I don't know how to get that data back to the calling class to use.
Thank you for your time, I hope you can point out what I am doing wrong here.