I have two files in my TypeScript application, namely:
File 1
and File 2
,
In File 1
, I want to save a value in local storage like this:
private load() {
return this.entityService
.load(this.$scope.projectRevisionUid)
.then(resp => {
localStorage.removeItem('employeerates');
this.$scope.employeeRates = resp.employeeRates;
return this.refreshCostRate(...resp.employeeRates)
.then(() =>
localStorage.setItem(
'employeerates',
JSON.stringify(this.$scope.employeeRates)
)
)
.then(() => this.refreshBillRate(...resp.employeeRates))
.then(() => resp.employeeRates.forEach(erm => this.calculate(erm)))
.then(() => DatepickerUtil.reinitializeDatepickers(this.$scope));
})
}
In File 2
, the following code exists:
const employeerates = JSON.parse(
localStorage.getItem('employeerates')
);
if (employeerates && employeerates.length != null) {
employeerates.forEach((element: any) => {
if (
this.employee.getUid() === element.user.personUid &&
element.internalRate
) {
this.cost_rate_uom = element.internalRate * this.uom_factor;
this.cost_rate_per_hour =
this.cost_rate_uom / this.uom_factor;
this.cost_rate.setValue(this.ap4_cost_rate_per_hour);
}
});
}
The issue here is that setting the local storage in File 1 is asynchronous, causing problems when trying to retrieve the data in File 2.
I am looking for a way to access the local storage value from File 1 in File 2 without using setTimeOut, as it has not solved my problem. Any suggestions on resolving this asynchronous data retrieval would be appreciated.
Update:
I have not found an alternative method of passing the data this.$scope.employeeRates
from File 1 to File 2, hence I resorted to using the local storage approach. However, due to the asynchronous nature of the function
this.refreshCostRate(...resp.employeeRates)
, I need to ensure that File 2 retrieves the correct value only after this function has completed.
If there are other ways to pass data between TypeScript files, especially post the
this.refreshCostRate(...resp.employeeRates)
function call, please provide your insights. It is crucial that File 2 receives the accurate this.$scope.employeeRates
value.