In order to achieve my goal, I initially utilized synched requests which made the process easier. However, now that this method is deprecated, I am in the process of updating my script to utilize asynchronous calls instead. This adjustment ensures that even if the previous method becomes obsolete, my script will continue to function smoothly.
The current code snippet I am working with is outlined below:
function GET(a) {
var request = new XMLHttpRequest(),
data;
request.open("GET", a, false);
request.onreadystatechange = function () {
if(4 == request.readyState) data = request.responseText;
};
request.send();
return data;
}
function getObject(a) {
var constructor = {};
constructor.property1 = 'something1';
constructor.property2 = 'something2';
constructor.property3 = 'something3';
constructor.property4 = true;
if(a && a.attribute === null) constructor.property5 = GET('/url-location');
return constructor;
}
var object = getObject(a);
if(object.property5) doSomethingWith(object);
Currently, the request is executed at runtime to assign a value to the constructor object. The synchronous nature of this setup allows the function to wait for a response before progressing, ensuring a value can be assigned prior to returning the object.
However, when running asynchronously, the function moves forward without waiting for the response, leading to complications.
My dilemma lies in whether it is feasible to replicate this behavior using asynchronous requests. If not, how can I attain a similar structure without extensive modifications? Specifically, the creation of the constructor object must remain as presented - setting multiple properties sequentially upon calling getObject and returning when completed.
I am unable to incorporate libraries such as jQuery or Node. Additionally, I aim to avoid timed/looped functions.
An important detail that should not go unnoticed: the GET function is employed by another function with a different configuration but serves a comparable purpose. Therefore, altering the GET function solely for the provided example is not a viable solution.