0

I get this function :

async function fetchData (name) { 
  let url = '/api/1?name=' + address; 
  let response = await fetch(url).then((data)); 
  let data = await response.json(); 
  return data.name; 
}  

console.log(fetchData('ABC'));

When I go to the console I see this :

Promise {<pending>}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[[PromiseResult]]: "Hello"

How can I just get the value of PromiseResult ? (Hello)

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 2
    `fetchData('ABC').then(res => {console.log(res)});` – Chin. Udara Aug 18 '21 at 13:49
  • Anything that needs the data must be in the context of the `await`ed promise or inside of a `.then` on the promise, all the way up. In other words, inside `fetchData` and after the `await`s, you have the data. But outside that context, you *don't* without another `await` or `.then`. – crashmstr Aug 18 '21 at 13:50
  • 3
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Chin. Udara Aug 18 '21 at 13:51
  • @Chin.Udara ok thanks ! if I just want to get this value in variable without using console.log ? – solidityDev Aug 18 '21 at 14:13

0 Answers0