forEach and map function not looping through all items in async-await in Vue.js

Viewed 282

I have an orders array that has a set of meal plans and each meal plan has items inside it.

Now what i am trying to achieve is when i click place orders all of the orders in the array would be processed as paid in the inner loop. but it is only processing some and leaving some behind.

here is my code:

 make_delivery_orders(){
            let self = this;
            var orders = this.deliveryPlans;
            swal('This process will place delivery orders! \nAre you sure you want to continue?' ,{
                icon: 'info',
                buttons: {
                    secondary:"Cancel",
                    danger:"Yes & Continue"
                },
            }).then(async(value) => {
                if(value === 'danger'){
                    orders.map((order,index) => {
                        if(order.included){
                            self.post(siteUrl + '/main/sync' ,   order  ,function(o){
                                if( parseInt(o.result) === 200) {
                                    let r = o.response;
                                    try{
                                        var push = {
                                            data: order,
                                        }
                                        console.log(order)
                                        var url = siteUrl+'/main/print_order_string/';
                                        self.post(url, push,function (res) {
                                            try {
                                                var text = res.response
                                                
                                                // to pay the meal in DB
                                                order.details.data[0].cart.map((item,index)=>{
                                                    // console.log(item)
                                                    app.mealTopay_array.push(item.meal_id);
                                                    setTimeout(function() {
                                                        app.pay_selected_meal(item.meal_id, 1)
                                                    }, 100);
                                                     app.pay_selected_meal(item.meal_id, 1)
                                                    
                                                    console.log(app.mealTopay_array)
                                                })
                                                
                                                // to print the receipt
                                                // app.print_text(text, order)
                                                // app.pay_meal_array()
                                            }catch(e){
                                                console.log(e)
                                            }
                                        });
                                    }catch(e){
                                        console.log(e)
                                    }
                                }
                            });
                        }
                    })
                    // .then((val)=>{
                        self.make_delivery_by_click('sdj')
                    // })
                }
            });
        },




   pay_selected_meal(id, val){
            let self = this;
            console.log(id)
            console.log(val)
            this.post(siteUrl+'/main/updateMealPaid/'+id+'/'+val,'',function (res) {
                try {
                    console.log(res)
                    // insertLog(log)
                }
                catch (e) {
                    console.error(e);
                }
    
                self.loading = false;
            });
        },
1 Answers

first I could not find any await inside the code snippet you shared plus, you can not use await inside javascript loops such as forEach or map For a map, you should wait for all the promises or use a traditional loop:

for (let index = 0; index < orders.length; index++) 

in order to use await inside it.

Related