ASYNC/AWAIT not working properly with Wix API Calls

Hi all,

Wix native API Calls that return promises seem to not care much about async/await in some cases.

consider the following code:

export async function buttonAddCart_click(event) {
 let quantityToAdd=0;
 let discountType="";
 let discountValue=0;
 let newDiscountType="PERCENT";
 let newDiscountValue=parseInt($w("#textDiscount").text, 10);
 let productIdToAdd="";
 let productName="";
 let bundleNameToAdd="";
 let sucess=false;
 let totalItems=$w("#datasetItems").getTotalCount();
 let result=await $w("#datasetItems").getItems(0,totalItems);
    productItems=result.items;
    bundleNameToAdd=$w("#textBundleName").text;
 for (var i = 0; i < totalItems; i++) {
        quantityToAdd=parseInt(productItems[i].quantity,10)*parseInt($w("#inputQuantity").value, 10);
        discountType=productItems[i].product.discount.type;
        discountValue=parseFloat(productItems[i].product.discount.value,10);
        productIdToAdd=productItems[i].product._id;
        productName=productItems[i].product.name;
        productsBeforeUpdate[i]={
 "productId":        productIdToAdd,
 "discountType":     discountType,
 "discountValue":    discountValue
        };
        productstoAdd[i]={
 "productId":    productIdToAdd,
 "quantity":     quantityToAdd,
 "options": {
 "choices": {
 "BottleSize":   "0.75"
                },
 "customTextFields": [{
 "title":    "Bundle",
 "value":    bundleNameToAdd
                }]
            }
        }
        productstoUpdate[i]={
 "productId":        productIdToAdd,
 "discountType":     newDiscountType,
 "discountValue":    newDiscountValue
        }       
    }

 //update discount for products in bundle
    console.log("update products discount to match bundle");
 await updateProductDiscountBundle(productstoUpdate);

 //Add products to Cart
    console.log("add updated products to cart");
 await addProductstoCart(productstoAdd);

 //revert products back
    console.log("revert products discount to original values");
 await updateProductDiscountBundle(productsBeforeUpdate);
}

export async function addProductstoCart (productstoAddParameter) {
    console.log("productstoAdd:");
    console.log(productstoAdd);
 try {
 await $w('#shoppingCartIcon1').addProductsToCart(productstoAddParameter)
    } 
 catch(error) {
        console.log(error.message);
    }   
}

export async function updateProductDiscountBundle (productstoUpdateParameter) {
 let productId="";
 let discountType="";
 let discountValue=0;
    console.log("productstoUpdateParameter:");
    console.log(productstoUpdateParameter);
 let totalProducts=productstoUpdateParameter.length;
 for (var i = 0; i < totalProducts; i++) {
        productId=productstoUpdateParameter[i].productId;
        discountType=productstoUpdateParameter[i].discountType;
        discountValue=productstoUpdateParameter[i].discountValue;
 await updateProductDiscount(productId,discountType,discountValue);      
    }   
}

function “updateProductDiscount” exists on a backend module:

import wixStoresBackend from 'wix-stores-backend';

export async function updateProductDiscount(productId, type, value) {
 let product={}
 try {
      product= await wixStoresBackend.updateProductFields(productId, {
 "discount": {
 "type":   type,
 "value":  value
          }
      });
      console.log("(Backend/updateProductDiscount) product updated successfully:");
      console.log(product);
 return true;          
    }
 catch (error) {
      console.log(error.message);
 return false;
    }
}

The problem is the code doesn’t wait for promises to resolve . I’m cycling through an array containing 3 products. I want to first update relevant discount prices for the three products, add them to cart, and revert back to original values. This is what I get on the console:

update products discount to match bundle

(Backend/updateProductDiscount) product updated successfully: <first product updated>

(Backend/updateProductDiscount) product updated successfully: <second product updated>

add updated products to cart  <should only happen after third product updated>

revert products discount to original values

(Backend/updateProductDiscount) product updated successfully: <third product updated. should have happened before "add">

(Backend/updateProductDiscount) product updated successfully: <first product reverted>

(Backend/updateProductDiscount) product updated successfully: <second product reverted>

(Backend/updateProductDiscount) product updated successfully: <third product reverted>

How can I make this work?

Thanks
José Caeiro