Javascript 如果api的响应为false,如何停止for循环?

Javascript 如果api的响应为false,如何停止for循环?,javascript,angular,for-loop,angular5,Javascript,Angular,For Loop,Angular5,如果api的响应为false,如何停止循环?。我有下面的api集成代码。现在api正在按循环一次调用。如果api的响应为真,我想调用相同的api。下面是我的代码 for (let i = 0; i < this.fooditemselecteddetails.length; i++) { this.spinnerService.hide(); //cons

如果api的响应为false,如何停止循环?。我有下面的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的响应为真,则再次使用此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();
                    }
                });

   }
将此函数称为


我不明白。你能解释一下吗?。我是后端新手,我知道在另一个函数中有循环。因此,您可以删除循环,并将此调用放在前面提到的函数中。它将进行递归调用,直到出现错误或所有项都已完成。因此,您的意思是将addRecessionsIndex作为另一个函数,该函数将在for循环的位置进行调用。是吗?没错,你明白了。是的,但这和我毫无关联:
this.addConcessions(this.fooditemselecteddetails.length-1);