首页 > 解决方案 > 如果来自 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 则仅在此处停止循环。

标签: javascriptangularfor-loopangular5

解决方案


为此,您需要以同步方式运行您的服务。

这是您可以进行的更改以按顺序执行代码

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);

推荐阅读