I call an api inside a forEach, then i push the result of the api in an array, the problem is that the array is empty outside of the forEach.
this.apiConvention.findConventionsRCSendedUsingPOST(this.exerciceFramework.specialities.map((speciality: any) => speciality.code)).subscribe((data: any[]) => {
if (data) {
let array: any[] = [];
data.forEach(convention => {
this.apiConvention.findAgreementFileByConventionUsingGET(convention.externalId, this.exerciceFramework.externalId).subscribe((agreementFile: any) => {
if (agreementFile) {
array.push(this.initData(convention, agreementFile));
}
})
})
console.log(array); // empty
}
})
I tries also with async and await, but the same problem remains
let data: any[] = await this.apiConvention.findConventionsRCSendedUsingPOST(this.exerciceFramework.specialities.map((speciality: any) => speciality.code)).toPromise();
if (data) {
let array: any[] = [];
data.forEach(async convention => {
let agreementFile: any = await this.apiConvention.findAgreementFileByConventionUsingGET(convention.externalId, this.exerciceFramework.externalId).toPromise();
if (agreementFile) {
array.push(this.initData(convention, agreementFile));
}
})
console.log(array); // empty
}