1

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;
            });
        },
Nikola Pavicevic
  • 21,952
  • 9
  • 25
  • 46
Mazin
  • 11
  • 1
  • 1
    As it was said, there's no await. You shouldn't mix async/await and raw promises together. It's unclear what's going on there for anybody including you. There is no foreach as well. Also see https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop – Estus Flask Jan 30 '22 at 11:55

1 Answers1

1

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.

Sarout
  • 821
  • 4
  • 25
  • can u give an example code? or a detailed answer? – Mazin Jan 30 '22 at 08:25
  • @Mazin here, read this article: https://zellwk.com/blog/async-await-in-loops/ . I faced this issue before and solved it with traditional loop. Read the forEach and map part carefully to understand it – Mehrad Alinezhad Jan 30 '22 at 08:27