I'm currently having issues when using Promise.all() for the first time (two get-methods). Currently, when I console log my response, I can see the data I'm trying to fetch under promiseValue in the response. But I'm not sure how to access it?
Heres my code:
getData: function () {
Promise.all([
fetch('@Url.Action("GetSomething")',
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(this.selectedValues)
}),
fetch('@Url.Action("GetSomethingElse")',
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(this.selectedValues)
})
]).then(responses => {
if (responses[0].ok && responses[1].ok) {
return responses.map(response => {
return response.json();
});
} else {
throw new Error(`Error ${response.status}`);
}
}).then(responseArray => {
console.log("LOG: ", responseArray);
}).catch(error => {
console.log(error);
});
},
After reading online for answers, there where a few suggestions to add another .then() which gave me the same result (two promises). I looked at related questions asked regarding this issue here on StackOverflow, but only got more confused. I have also tried doing the following which also gave the same results:
...
}).then(responseArray => {
console.log("LOG: ", responseArray);
Promise.resolve([responseArray[0], responseArray[1]]).then((results) => {
console.log("Resolved results: ", results);
});
}).catch(error => {
console.log(error);
});
I'm currently stuck! This is what the console.log shows in the response:
LOG: (2) [Promise, Promise]
>0: Promise: {<Resolved>: (Array(20)}
>1: Promise: {<Resolved>: (Array(10)}
Help is much appreciated and I'm hoping there is a minor detail I am missing. How can I access these promises? Thanks!