I have a js object, and I have a method that calls another method and gets returned a promise, but from within .then() I cannot access a member function foo(). Why can't I access foo() and how can I access it? Here is my code:
function LoadImageGroupFormat() {
return new Promise(function(resolve, reject) {
var xhttp = new XMLHttpRequest();
xhttp.open('GET', "imagegroup_format.txt");
xhttp.onload = function() {
if (xhttp.status == 200) resolve(xhttp.responseText);
else reject(Error(xhttp.statusText));
};
xhttp.onerror = function() {
reject(Error("Network Error"));
};
xhttp.send();
});
}
//the object
var Registerhandler = {
imageGroupIndex: 0,
foo: function() {
//Do something else here
},
GetImageGroupFormat: function() {
var imageGroupIndex = this.imageGroupIndex;
LoadImageGroupFormat().then(function(ImageGroup_Format) {
//Do something with ImageGroup_Format
imageGroupIndex++; //This works
foo(); //Doesn't work - undefined
}, function(error) {
console.error("Failed to Load ImageGroup Format", error);
});
}
}
Thanks for any help on this.