Hi @dmlkl
It’s a common async problem: all the calls made to this.requestService
are asynchronous, but the loop keeps executing. In my experience, the cleanest way to make this async loop synchronous is by using async/await and waiting for the promises to resolve:
async function GetGeneralInfo() {
for (let i = 0; i < deviceData.length; i++) {
console.log('Loop Start')
const telemetryData = await this.requestService.GetTelemetryData(loginAcc.token, deviceData[i].id.id);
console.log('After GetTelemetryData()');
const status = JSON.parse(telemetryData.status);
if (status === 401) {
console.log('Token Expired...');
const refreshTokenResponse = await this.requestService.RefreshToken(loginAcc.token, loginAcc.refreshToken);
//i need to terminated the for loop and recall this method with new token
return this.GetGeneralInfo();
} else if (status === 200) {
console.log('Token is valid and data is given....');
const data = JSON.parse(telemetryData.data);
}
}
}
Hope it helps,
Rodrigo