Kristin Chia
Kristin Chia

Reputation: 11

How to get and display the value of Promise Result in react JS?

I am trying to extract data returned from promise and have the data be displayed on my react app page e.g., within a div. However, it doesn't seems to work.

As shown in the code below, I was able can get res value "8" which I needed under console.

Then, I intended to use "count" and put it within a div to display its result in the webpage. However, it doesn't work as it gives Promise.

  let count = getNumScopeStatus("TOBESAMPLED").then((res) => {
    return res;
    //console.log(res); <-- able to get "8"
  });

  console.log({count}); //gives Promise

Thus, how do I to get the promise result/res value "8" and place it for instance within a div to display it on webpage?

      <div className="result">
        {count} //wants to display the value 8
      </div>

Upvotes: 1

Views: 966

Answers (1)

Take a look on async/await.

In this case, you can use it:

let count = await getNumScopeStatus("TOBESAMPLED")

After it, you will be able to use count on everywhere.

Upvotes: 2

Related Questions