This code is from a React app. The makeCall function was provided in a Twitch extension example project. I'm using it to call the backend server for my extension.
makeCall(url, method="GET"){
return new Promise((resolve, reject)=>{
if(this.isAuthenticated()){
let headers={
'Content-Type':'application/json',
'Authorization': `Bearer ${this.state.token}`
}
fetch(url,
{
method,
headers,
})
.then(response=>resolve(response))
.catch(e=>reject(e))
}else{
reject('Unauthorized')
}
})
}
I can access the response, but I'm not certain what's going on with this line of code above: .then(response=>resolve(response))
. When I call makeCall
it's returning a promise, but the response I'm accessing in the below is showing a Promise inside the response, and I'm not sure how to access the value in there:
this.Authentication.makeCall(bitpostConstants.GET_STATUS_URL)
.then(response=>{
if(response.status!=200) {
console.log('Error getting status.');
} else {
var responseJson = response.json();
console.log('Response', responseJson);
// How do I access 'twitterConnected'? This is showing as undefined.
console.log('Twitter connected: ' + responseJson.twitterConnected);
Below is the log to the browser dev tools, showing the property (twitterConnected) I'm trying to access, but shows up as undefined if I try to access it above with responseJson.twitterConnected
:
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Object
twitterConnected: false
__proto__: Object
Twitter connected: undefined