Is there a way to access an array that is within the PromiseValue
instead of receiving Promise {<pending>}
When I use
, I can see the array in the console log. However, my requirement is to display this array on an HTML page. So, I modified the code to .then(data => console.log(data))
.then(data => data)
and now it returns Promise {<pending>}
const baseUrl = 'http://localhost:3000';
function sendRequest(method, url, data = null) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.responseType = 'json';
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = () => {
if (xhr.status >= 400) {
reject(xhr.response);
} else {
resolve(xhr.response);
}
}
xhr.onerror = () => {
reject(xhr.response);
}
xhr.send(JSON.stringify(data));
});
}
let getData = sendRequest('GET', baseUrl + '/users')
.then(data => data)
.catch(err => console.log(err));
console.log(getData);
I appreciate any help with this issue.