Initializing arrays with the call this.Reload.All()
is causing confusion and breaking the service due to multiple asynchronous calls. I am looking for a synchronous solution where each call waits for its response before proceeding to the next one. How can I achieve this?
Although my code is functioning correctly, the async nature of the calls is causing issues. How can I resolve this issue?
//Reload Reference Arrays
RefVehicleTypes = [];
RefClients = [];
RefUsers = [];
RefObjects = [];
//Reload Class:
Reload = {
DoCall : (pUrl, pArray, pResponse) => {
this.HttpClient.get(pUrl).subscribe(
(response: any) => {
this[pArray] = eval(pResponse);
console.log(pResponse + ' : ' + this[pArray]);
}
);
},
All : () => {
this.Reload.VehicleTypes();
this.Reload.Clients();
this.Reload.Users();
this.Reload.Objects();
},
VehicleTypes : () => {
this.Reload.DoCall(
'http://...',
'RefVehicleTypes',
'response.dbVehicleType_response.view',
);
},
Clients : () => {
this.Reload.DoCall(
'http://...',
'RefClients',
'response.dbClient_response.view',
);
},
Users : () => {
this.Reload.DoCall(
'http://...',
'RefUsers',
'response.dbUser_response.view'
);
},
Objects : () => {
this.Reload.DoCall(
'http://...',
'RefObjects',
'response.dbObject_response.view'
);
}
}
NEW EDIT:
I attempted to replace the DoCall and All methods, but the issue persists. Please refer to the screenshot below for more information.
DoCall : (pUrl, pArray, pResponse) => {
return new Promise((resolve, reject) => {
this.HttpClient.get(pUrl).subscribe(
(response: any) => {
console.log('##############################' + pArray)
console.log(this[pArray]);
this[pArray] = eval(pResponse);
console.log(this[pArray]);
resolve();
}
)
});
},
All : async () => {
await this.Reload.VehicleTypes();
await this.Reload.Clients();
await this.Reload.Users();
await this.Reload.Objects();
},
The screenshots show 3 refreshes where it seems like the async function All() is mixing up the arrays, causing delays in responses. The last refresh always works fine, indicating that previous responses might be getting overwritten by subsequent ones.