title: Javascript: object cannot be updated
body: <p>I have an object named &quot;order&quot;, and I am trying to change some values within in. Below is the code that I use, but it is showing me some weird behaviour.</p>
  <p>All console.log output are included as comments after the corresponding line. As far as I can tell, the code is working because when I try to console.log the new value, it is correctly printed on the console.</p>
  <p>The problem happens at my last line of code. When I am printing the entire object on console, and navigate to the two values that I just changed, they are both showing the old value &quot;0.60&quot;, instead of the new value &quot;1.00&quot;.</p>
  <p>What is causing this bug?</p>
  <pre><code>console.log(&quot;this.calculate_discount(product)&quot;, this.calculate_discount(product)); // 0.40
  console.log(&quot;order.SubTotalDiscount&quot;, this.order.SubTotalDiscount); // 0.60
  this.order.SubTotalDiscount = parseFloat(this.calculate_discount(product)) +
                                parseFloat(this.order.SubTotalDiscount);
  this.order.SubTotalDiscount = parseFloat(this.order.SubTotalDiscount).toFixed(2);
  console.log(&quot;order.SubTotalDiscount&quot;, this.order.SubTotalDiscount); // 1.00
  console.log(&quot;order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount&quot;,
                  this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount); // 0.60
  this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount = 
                  parseFloat(this.calculate_discount(product)) +
                  parseFloat(this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount);
  this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount =
                  parseFloat(this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount).toFixed(2);
  console.log(&quot;order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount&quot;,
                  this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount); // 1.00
  console.log(&quot;order&quot;, this.order);
  </code></pre>
  <p>Thank you!</p>
  <p>Update 2022-02-25</p>
  <p>After some debugging, I have found that the problem is related to async(). This block of code is within an async(), and I have used await() after this block of code. I thought javascript runs from top to bottom, so I never thought this would be the cause of the problem.</p>
  <pre><code>async RedeemByPoints(product, index){
    this.selected_product(product);
    if(document.getElementById(&quot;btn_redeem_text_&quot; + index).innerHTML == &quot;Revert&quot;){
      // DO SOMETHING...
      console.log(&quot;this.calculate_discount(product)&quot;, this.calculate_discount(product)); // 0.40
      console.log(&quot;order.SubTotalDiscount&quot;, this.order.SubTotalDiscount); // 0.60
      this.order.SubTotalDiscount = parseFloat(this.calculate_discount(product)) +
                                    parseFloat(this.order.SubTotalDiscount);
      this.order.SubTotalDiscount = parseFloat(this.order.SubTotalDiscount).toFixed(2);
      console.log(&quot;order.SubTotalDiscount&quot;, this.order.SubTotalDiscount); // 1.00
      console.log(&quot;order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount&quot;,
                  this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount); // 0.60
      this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount = 
                  parseFloat(this.calculate_discount(product)) +
                  parseFloat(this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount);
      this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount =
                  parseFloat(this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount).toFixed(2);
      console.log(&quot;order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount&quot;,
                  this.order.totals[TOTAL_CODE_MAP.get('discount_b')].Amount); // 1.00
      console.log(&quot;order&quot;, this.order);
      // DO SOMETHING...
      var taxes = await this.calculate_product_taxes(this.selected_item);
      // DO SOMETHING...
    } else {
      // DO SOMETHING...
    }
  }
  </code></pre>
  <p>Even if I comment out the await() line, the problem persist. It will only disappear if I change the function to a normal function instead of an async(). But why?</p>
  <p>As suggested by @MetBr , I am including all the objects and variables used in this script.</p>
  <pre><code>product{
    Deleted: (...)
    Discounted: (...)
    IsTakeOut: (...)
    ItemDiscountAmount: (...)
    ItemDiscountName1: (...)
    ItemDiscountName2: (...)
    ItemDiscountType: (...)
    ItemDiscountValue: (...)
    ItemNote: (...)
    ItemSubTotal: (...)
    ItemTaxTotalAmount: (...)
    K: (...)
    MinQty: (...)
    ModifierAmount: (...)
    ModifierString: (...)
    Name1: (...)
    Name2: (...)
    NoDiscount: (...)
    OnHold: (...)
    OrderDiscountAmount: (...)
    OrderDiscountType: (...)
    OrderDiscountValue: (...)
    PriceGroup: (...)
    PriceOverrideFlag: (...)
    PriceOverrided: (...)
    ProductIDn: (...)
    Qty: (...)
    Receipted: (...)
    ReferenceItemID: (...)
    SalePrice: (...)
    SelectedModifiers: (...)
    Status: (...)
    TaxTypeIDn: (...)
    Taxes: (...)
    TransactionNo: (...)
    TransactionType: (...)
    Type: (...)
    UnitPrice: (...)
    UnixTimestamp: (...)
    id: (...)
    modifiers: (...)
    sortN: (...)
  }
  
  order{
    ComputerName: (...)
    CustomerCount: (...)
    CustomerIDn: (...)
    DollarDiscount: (...)
    ExtraPoint: (...)
    IDn: (...)
    MenuIDn: (...)
    OrderDiscount: (...)
    OrderNote: (...)
    Printed_1: (...)
    Printed_2: (...)
    Printed_3: (...)
    ReferenceTransactionNo: (...)
    ReloadBalance: (...)
    ReloadPoint: (...)
    Status: (...)
    StoreName: (...)
    SubTotalAmount: (...)
    SubTotalDiscount: (...)
    TableIDn: (...)
    ToGo: (...)
    TotalAmount: (...)
    TotalItemCount: (...)
    TransactionNo: (...)
    Type: (...)
    UnixTimestamp: (...)
    UserIDn: (...)
    cur_customer: (...)
    id: (...)
    items: (...)
    payments: (...)
    reloadFlag: (...)
    reloadProductIDn: (...)
    totals: (...)
  }
  
  TOTAL_CODE_MAP.get('discount_b') will return 1
  </code></pre>