My current issue involves a loop in which a method is called, and the method returns an object of type Promise<any>
. I need to break the loop if the response from the method is correct. However, using the break
statement does not stop the loop as expected:
for (let item of List) {
let currentItem = item.split(',');
if (flag != '0')
{
break;
}
this.service.getList(currentItem[0]).then(res => {
if (res != null) {
for (let i of res) {
if (i.serviceName == name) {
flag = selectIp;
break;
}
}
}
}).catch(res => {
});
}
The relevant code from the Service:
getList(ip: string): Promise<any> {
const apiUrl = environment.url + '/getData';
return this.httpClient.post<any>(apiUrl, body)
.toPromise();
}
The issue I am facing is that the break
statement is not functioning correctly. The loop continues even when both flag != '0'
and i.serviceName == name
.