I'm trying to learn how to replace callback functions with async and await. After two days I have the following working in that it will write the json to the console from inside the function.
const requestRoster = async ()=> {
const response = await fetch('/testing/getRoster.php', {
method: 'get',
headers: {
'Content-Type': 'application/json'
}
})
const json = await response.json()
console.log(json); // writes the array json to the console
return json; //apparently returns a pending promise
}
However, when I say
$roster = requestRoster();
console.log ($roster); // writes Promise{<pending}> to the console
The console reports
Promise {} When I expand this line, I see:
Promise {<pending>}
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Array(64)
and Array(64) contains the data I want.
Obviously I'm a bit lost here. Apparently the function requestRoster() is returning a pending promise. What I want is for it to return the Array(64). So, where am I going wrong? I simply want requestRoster() to return the Array(64)
Thanks,