Search code examples
javascriptasynchronousasync-awaitfetch

Problem when trying to fetch data from the Fetch API. Empty parentheses arrive before clicking in the console


I'm trying to get a data from fetch API, but my result is empty before I click on result in console. And I couldn't manage this data in my app. I don't have something, problem with asynchronous getting data from fetch.

I tried to use other asynchronous methods to receive data, but all to no avail.

enter image description here

enter image description here


Solution

  • If the actual method that gets remote data is async you need to make every method in your stack async and then await each return value. That's why you are seeing the default value. For example...

    // constroller.js
    async getDataFromModel() {
      this._data = await model.getData();
      console.log(this._data);
    }
    // weatherModel.js
    async getData() {
      return await this.getPosition();
    }