javascript - 如果来自 api 的响应为假,如何停止 for 循环?
问题描述
如果来自 api 的响应为假,我如何停止循环?我有以下用于 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();
}
});
}
AddConcessions?
如果此 api 的响应为真,我想再次调用此 api。如果它返回 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();
}
});
}
将此函数称为
this.addConcessions(this.fooditemselecteddetails.length-1);
推荐阅读
- python - 在 Community Edition 中的 DBFS 位置遍历数据块中 pyspark 中的文件
- java - Java-Zoned 时间没有返回正确的年份
- php - SQL 更新后 PHP 通过 if 语句循环返回
- android - Google Play 控制台:我的应用程序的活跃用户数低于安装数。怎么会这样?
- spring-boot - Spring Webflux -Security:当jwt令牌过期或错误时如何让Spring返回401(未授权)异常
- javascript - 如何使用类更改div内所有按钮的tabindex
- angular - Nativescript Angular 错误:无法使用 TabStrip 匹配任何路由
- javascript - 我可以使用 map 函数的索引作为包含动态不可预测文本的 React 组件的键吗?
- hibernate - 导入 quarkus-hibernate-orm 后如何使用 Oracle 数据库配置 Quarkus?
- selenium - 如何使用 NUnit 中的 TestCaseSource 属性限制对各个测试方法的测试数据方法调用