scuba_mike
scuba_mike

Reputation: 409

At an object's instantiation time how does one handle best the asynchronous initialization of one of its properties?

I've never created a Javascript module/library before so this is a bit new to me so apologizes for my lack of knowing what to google.

I'm creating a library that will hold information from a URL that is provided by a user. I want to parse the URL's path (the part that comes after the domain) as well as retain a header value that's provided by the URL's response.

It's basic but here's what I have so far:

function Link(someURL) {
  this.url = someURL;
  this.urlPath = "";
  this.uuid = "";

  this.getPath = function (someURL) {
    // do regexp parsing and return everything after the domain
  };

  this.getUUID = function (someURL) {
    // fetch the URL and return what is in the response's "uuid" header
  }
}

Ideally, I'd the module to automatically get all the information upon construction:

var foo = new Link("http://httpbin.org/response-headers?uuid=36d09ff2-4b27-411a-9155-e82210a100c3")
console.log(foo.urlPath);  // should return "uuid"
console.log(foo.uuid);  // should return the contents in the "uuid" header in the response

How do I ensure the this.urlPath and this.uuid properties get initialized along with this.url? Ideally, I'd only fetch the URL once (to prevent rate limiting by the target server).

Upvotes: 1

Views: 152

Answers (1)

scuba_mike
scuba_mike

Reputation: 409

After a lot of trial and error, I ended up doing something more like this:

class Link {
  constructor (url_in) {
    const re = RegExp("^https://somedomain.com\/(.*)$");
    this.url = re[0];
    this.linkPath = re[1];
  }

  async getUUID() {
    const res = await fetch("https://fakedomain.com/getUUID?secret=" + this.linkPath);
    this.uuid = res.uuid;
  }

  async getJSON() {
    const res = await fetch("https://fakedomain.com/getJSON?uuid=" + this.uuid);
    this.json = await res.json();
  }

  async initialize() {
    await this.getUUID();
    await this.getJSON();
  }
}


const someLinkData = new Link("https://reallydumbdomain.com/2020/10/4/blog");
someLinkData.initialize()
  .then(function() {
    console.log(this.json); // this now works
  });

I think a future iteration of this will require me to send a promise with the initialize function but for now, this works.

Upvotes: 1

Related Questions