When exporting a CSV file in my code, I encounter a race condition while trying to modify some data before the export. The issue is that the id gets set correctly, but the number only updates after the method is called a second time. I believe the problem lies in the fact that the return statement is being executed before the observable has finished. How can I resolve this issue?
public exportAsCSV(
arr: ArrEntity[],
origin: string,
id?: string,
): Observable<Blob> {
let headers = new HttpHeaders();
if (arr && arr.length > 0) {
headers = headers.append(
xxx
);
if (origin === 'table') {
this.getValues(3).subscribe(() => {
for (const a of arr) {
a.id = id; // works fine
a.number = this.getNumberById(a.number); // only correct after 2nd call
}
});
}
return this.http.post(
url,
{
chargeLogEntities: arr,
},
{
headers: headers,
responseType: 'blob',
},
);
} else {
return Observable.of();
}
}