如果api的响应为假,如何停止循环?

时间:2018-10-20 07:32:26

标签: javascript angular for-loop angular5

如果api响应为假,如何停止循环?我有以下用于api集成的代码。现在,api一次按循环调用。如果来自api的响应为true,我想调用相同的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。如果返回false,则仅在此处停止循环。

1 个答案:

答案 0 :(得分:1)

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

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

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