1
updateGraphCurrentIndex(value) {
      // Returns a Promise that resolves after "ms" Milliseconds
      const timer = function(ms) {
        return new Promise(res => setTimeout(res, ms));
      };

      this.$vs.loading({ container: "#compare-chart", type: "point" });

      console.log("///////selected TAB Recieved", value
        ,"///////// tickersss", this.selectedTickers);

      this.current_index = value;

      // clear data on Graph
      this.selectedTickers.forEach((ticker) => {
        this.clearTickerDataGraph(ticker);
      });

      // display data on the table

      console.log("////////selectedTikerss", this.selectedTickers[0]);
      async function load() {
        try {
           console.log("////////WHAT IS THIS", this);
          for (let index = 0; index < this.selectedTickers.length; index++) {
            console.log("Helllo ", index);
            console.log("////////////i is called here", this.selectedTickers[index]);
            this.tickerDataGraph(this.selectedTickers[index]);
            await timer(3000);
          }
        } catch (error) {
          console.log("///////////error in Load", error);
        }
      }
     
      load();

      // this.selectedTickers.forEach((ticker, i) => {
      //   setTimeout(()=> {
      //     this.tickerDataGraph(ticker);
      //     // await timer(3000); // then the created Promise can be awaited
      //   }, 3000);
      // });

      // ['IBM', 'APPL']
      // this.selectedTickers.forEach((ticker) => {
      //   this.tickerDataGraph(ticker);
      // });

    }

I want my load method to run after 3 seconds but as the flow of execution enters the load method, I get this error Cannot read properties of undefined (reading 'selectedTickers'). Actually, this gets undefined inside the async load method. The this.tickerDataGraph(this.selectedTickers[index]) contains api calls that I want to delay.

Abdullah Ch
  • 1,678
  • 1
  • 13
  • 31
  • 2
    Indeed, value of `this` for regular functions varies based on how you call the function. So to get correct `this`, you can convert to use arrow function: `const load = async () => { ... }` or you can explicitly give correct `this` when you invoke function: `load.call(this)`. See also [this question + answers](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – CRice Mar 02 '22 at 20:37

0 Answers0