Javascript set delays on return [duplicate]

I have a function as below. The code works fine but the issue is second console.log executes at first and then the first console.log. Which means i am supposed to return null but its returning mtd array because the ac=false inside the else statement executes slow. Because of that second console .log executes at first. How can i make this work in order? Please give me suggestion on how to fix this.

 newQuoteInventoryCheck(newData){
        let mtd = [];
        let ac = true;
        for (let i = 0; i < (newData.product_id).length; i++) {
             let added_quantity = newData.quantity[i];
             let newItem = newData.product_id[i];
             if(added_quantity > 0){
                
                Api.get('materials/get/'+newItem).then((data)=>{
                    let available_quantity = data.quantity;
                    if (added_quantity <= available_quantity){
                        mtd.push([newItem, (available_quantity - added_quantity)]);
                    }
                    else{
                        ac = false;
                        mtd.push([newItem, 0]);
                        $('#maxquantityavailable').text('Total quantity available for "'+ data.name+'" in inventory is: '+available_quantity);
                        $('#maximum-quantity-exceeded-modal').modal('show');
                    }
                    console.log('first: '+ ac) //first log
                });
                
            }
        }
         console.log('Second: '+ ac) //second log
         if(ac){
             return mtd;
         }
         else{
             return null;
         }           
},