With JavaScript, when creating a class, while instantiating that class, I want to populate a public property. I can do this using a setter, however, the value I want comes from an external website which I retrieve via an ajax get call. The issue becomes that the new class object does not have the appropriate property value when I create it. Here's some sample code:
class MyTestClass {
  constructor() {
    this._ipaddress = "";
  }
  get ip() {
    return this._ipaddress;
  }
  set ip(value) {
    createIpAddr();
  }
  createIpAddr() {
    var myIpAddr = "";
    var strUrl = "https://api.ipify.org/?format=json";
    $.ajax({
      url: strUrl,
      success: function(data) {
        this._ip = data.ip;
      },
      //async:false //Performing this in sync mode to make sure I get an IP before the rest of the page loads.
    });
    return myIpAddr;
  }
}
var testobj = new MyTestClass();
console.log(testobj.ip);The problem here is that I can't be sure the IP will be populated in time to use after creating the new instance of the class. I've tried promises and deffered, but they have the same problem, I can't populate the variable before I need it. I'm trying to adjust the way I am looking at this and adding callbacks, but the issue is that I need the correct value in the class before I can use the class for the next call, where I am passing this object to it.
Is there a simple solution I am over looking? I have been through a million of these threads about async: false, and I don't want to start a new one, but what is a better choice in this case?
I want to set a class property from an ajax response when instantiating the class object.
 
    