I have created an AJAX promise and when it is complete I am trying to set a class variable but it is not setting for some reason. I thought this was an ASYNC issue but the promise should have resolved that issue. It’s possible it is a scope issue but I don’t know how to resolve it.
$(document).ready(function(){
class Tables {
constructor() {
Tables.Action = this.GetAction()
Tables.Headers = ''
this.InitHeaders()
console.log(Tables.Headers)
//this.InitDatables()
}
GetAction() {
var Segments = window.location.pathname.split("/")
var URL = '/'+Segments[1]
return URL
}
InitHeaders(){
this.GetHeaders().then(function(data) {
// Run this when your request was successful
Tables.Headers = data
}).catch(function(err) {
// Run this when promise was rejected via reject()
console.log(err)
})
}
GetHeaders()
{
return new Promise(function(resolve, reject) {
$.ajax({
url : Tables.Action +'/GetHeaders',
success: function(data) {
resolve(data) // Resolve promise and go to then()
},
error: function(err) {
reject(err) // Reject the promise and go to catch()
}
})
});
}
}
let Table = new Tables();
});