I have recently developed a small Angular application that retrieves data from a REST API using observables. However, I am facing an issue with their asynchronous nature.
let tempArray: boolean[] = [];
for (let i = 0; i < 3; i++) {
this._myservice.checkData(data[i]).subscribe(
result => {
tempArray.push(result);
console.log('Before');
console.log(tempArray);
},
error => console.log(error),
);
}
console.log('After');
console.log(tempArray);
The problem arises when the result data does not end up in the correct array after the subscription, as shown in the screenshot below. Is there a way to resolve this issue without having to relocate all the code inside the subscription block?