Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140762

Why is promise from fetch not resolving (react)

$
0
0

I am new to promises and async/await in react. I have been reading but still, I don't understand why the promise from my getDate() method doesn't resolve.

I have been tying many solutions but it all ends the same, with an unresolved promise. Here is the code I'm using.

async getData() {
  await this.syncLedgerSum(
    "http://localhost:5000/webapi/getorders"
  ).then(response => this.setState({ items: response }));

  //this works, state.items is displayed on browser console
  //console.log("State Items in getData is : ", this.state.items);
}

async syncLedgerSum(URL) {
  const response = await fetch(URL, {
    method: "GET"
  });
  console.log("syncLedgerSum Response: ", response);

  const json = await response.json();
  console.log("syncLedgerSum json: ", json);
  return json;
}

async componentDidMount() {
    console.log("Component Did mount: ");
    let data = await this.getData();
    if (data) console.log("Items in Component Did mount is : ", data);
}

The image above shows the logs from the code I'm using.

enter image description here

I don't know why the request doesn't finish. How can I store the data into state.items to render that information?


Viewing all articles
Browse latest Browse all 140762

Latest Images

Trending Articles



Latest Images