如果 api 的响应为假,如何停止循环?
How to stop for loop if response from api is false?
如果 api 的响应为假,我该如何停止 for 循环?。我有以下用于 api 集成的代码。现在 api 正在按循环一次调用。如果 api 的响应为真,我想调用相同的 api。下面是我的代码
for (let i = 0; i < this.fooditemselecteddetails.length; i++) {
this.spinnerService.hide();
//console.log(this.fooditemselecteddetails);
this.common.createAPIService('api/booking/AddConcessions?CinemaId=' + this.cinemaid + '&TransactionId=' + this.temptransaction.TransactionId + '&ItemId=' + this.fooditemselecteddetails[i].id + '&Quantity=' + this.fooditemselecteddetails[i].quantity + "&BookingId=" + this.transactionAPIRequest.ORDER_ID, '').subscribe((result: any) => {
this.spinnerService.hide();
this.addconcession = result;
console.log(this.addconcession);
if (this.addconcession.IsSuccess == true) {
if (i == this.fooditemselecteddetails.length - 1) {
localStorage.setItem("bookingid", this.transactionAPIRequest.ORDER_ID);
this.common.createAPIService('api/booking/FinalBookingDetails?BookingId=' + this.transactionAPIRequest.ORDER_ID, '').subscribe((result2: any) => {
this.vistavalidation = result2;
if (this.vistavalidation.BookingID > 0) {
this.common.createAPIService('api/booking/ContinueTransaction?CinemaId=' + this.cinemaid + '&TransactionId=' + this.temptransaction.TransactionId, '').subscribe((result3: any) => {
if (result3.IsSuccess) {
this.ContinueTransactionresult = result3;
this.showTabOnClick('tabs-4');
}
else {
this.common.ShowNotification("Food Item", result3.Error, "info");
this.spinnerService.hide();
}
});
}
else {
this.common.ShowNotification("Food Item", 'something went wrong, please try again', "info");
this.spinnerService.hide();
}
});
}
}
else {
this.common.ShowNotification("Food Item", result.Error, "error");
this.spinnerService.hide();
}
});
}
如果此 api 的响应为真,我想再次致电 AddConcessions?
此 api。如果它是 returns false 则只在那里停止循环。
为此,您需要 运行 以同步方式提供您的服务。
这是您可以进行的更改以按顺序执行代码
addConcessions(i) {
this.spinnerService.hide();
//console.log(this.fooditemselecteddetails);
this.common.createAPIService('api/booking/AddConcessions?CinemaId=' + this.cinemaid + '&TransactionId=' + this.temptransaction.TransactionId + '&ItemId=' + this.fooditemselecteddetails[i].id + '&Quantity=' + this.fooditemselecteddetails[i].quantity + "&BookingId=" + this.transactionAPIRequest.ORDER_ID, '').subscribe((result: any) => {
this.spinnerService.hide();
this.addconcession = result;
console.log(this.addconcession);
if (this.addconcession.IsSuccess == true) {
if (i == this.fooditemselecteddetails.length - 1) {
localStorage.setItem("bookingid", this.transactionAPIRequest.ORDER_ID);
this.common.createAPIService('api/booking/FinalBookingDetails?BookingId=' + this.transactionAPIRequest.ORDER_ID, '').subscribe((result2: any) => {
this.vistavalidation = result2;
if (this.vistavalidation.BookingID > 0) {
this.common.createAPIService('api/booking/ContinueTransaction?CinemaId=' + this.cinemaid + '&TransactionId=' + this.temptransaction.TransactionId, '').subscribe((result3: any) => {
if (result3.IsSuccess) {
this.ContinueTransactionresult = result3;
this.showTabOnClick('tabs-4');
index--;
if(index >= 0){
this.addConcessions(index);
}
}
else {
this.common.ShowNotification("Food Item", result3.Error, "info");
this.spinnerService.hide();
}
});
}
else {
this.common.ShowNotification("Food Item", 'something went wrong, please try again', "info");
this.spinnerService.hide();
}
});
}
}
else {
this.common.ShowNotification("Food Item", result.Error, "error");
this.spinnerService.hide();
}
});
}
call this function as
this.addConcessions(this.fooditemselecteddetails.length-1);
如果 api 的响应为假,我该如何停止 for 循环?。我有以下用于 api 集成的代码。现在 api 正在按循环一次调用。如果 api 的响应为真,我想调用相同的 api。下面是我的代码
for (let i = 0; i < this.fooditemselecteddetails.length; i++) {
this.spinnerService.hide();
//console.log(this.fooditemselecteddetails);
this.common.createAPIService('api/booking/AddConcessions?CinemaId=' + this.cinemaid + '&TransactionId=' + this.temptransaction.TransactionId + '&ItemId=' + this.fooditemselecteddetails[i].id + '&Quantity=' + this.fooditemselecteddetails[i].quantity + "&BookingId=" + this.transactionAPIRequest.ORDER_ID, '').subscribe((result: any) => {
this.spinnerService.hide();
this.addconcession = result;
console.log(this.addconcession);
if (this.addconcession.IsSuccess == true) {
if (i == this.fooditemselecteddetails.length - 1) {
localStorage.setItem("bookingid", this.transactionAPIRequest.ORDER_ID);
this.common.createAPIService('api/booking/FinalBookingDetails?BookingId=' + this.transactionAPIRequest.ORDER_ID, '').subscribe((result2: any) => {
this.vistavalidation = result2;
if (this.vistavalidation.BookingID > 0) {
this.common.createAPIService('api/booking/ContinueTransaction?CinemaId=' + this.cinemaid + '&TransactionId=' + this.temptransaction.TransactionId, '').subscribe((result3: any) => {
if (result3.IsSuccess) {
this.ContinueTransactionresult = result3;
this.showTabOnClick('tabs-4');
}
else {
this.common.ShowNotification("Food Item", result3.Error, "info");
this.spinnerService.hide();
}
});
}
else {
this.common.ShowNotification("Food Item", 'something went wrong, please try again', "info");
this.spinnerService.hide();
}
});
}
}
else {
this.common.ShowNotification("Food Item", result.Error, "error");
this.spinnerService.hide();
}
});
}
如果此 api 的响应为真,我想再次致电 AddConcessions?
此 api。如果它是 returns false 则只在那里停止循环。
为此,您需要 运行 以同步方式提供您的服务。
这是您可以进行的更改以按顺序执行代码
addConcessions(i) {
this.spinnerService.hide();
//console.log(this.fooditemselecteddetails);
this.common.createAPIService('api/booking/AddConcessions?CinemaId=' + this.cinemaid + '&TransactionId=' + this.temptransaction.TransactionId + '&ItemId=' + this.fooditemselecteddetails[i].id + '&Quantity=' + this.fooditemselecteddetails[i].quantity + "&BookingId=" + this.transactionAPIRequest.ORDER_ID, '').subscribe((result: any) => {
this.spinnerService.hide();
this.addconcession = result;
console.log(this.addconcession);
if (this.addconcession.IsSuccess == true) {
if (i == this.fooditemselecteddetails.length - 1) {
localStorage.setItem("bookingid", this.transactionAPIRequest.ORDER_ID);
this.common.createAPIService('api/booking/FinalBookingDetails?BookingId=' + this.transactionAPIRequest.ORDER_ID, '').subscribe((result2: any) => {
this.vistavalidation = result2;
if (this.vistavalidation.BookingID > 0) {
this.common.createAPIService('api/booking/ContinueTransaction?CinemaId=' + this.cinemaid + '&TransactionId=' + this.temptransaction.TransactionId, '').subscribe((result3: any) => {
if (result3.IsSuccess) {
this.ContinueTransactionresult = result3;
this.showTabOnClick('tabs-4');
index--;
if(index >= 0){
this.addConcessions(index);
}
}
else {
this.common.ShowNotification("Food Item", result3.Error, "info");
this.spinnerService.hide();
}
});
}
else {
this.common.ShowNotification("Food Item", 'something went wrong, please try again', "info");
this.spinnerService.hide();
}
});
}
}
else {
this.common.ShowNotification("Food Item", result.Error, "error");
this.spinnerService.hide();
}
});
}
call this function as
this.addConcessions(this.fooditemselecteddetails.length-1);