Javascript 如何使用Promise.allsolited访问fetch/http调用的响应值?
我正在尝试使用Promise.AllSetted调用3个URL,并在完成所有操作后处理结果。但是,我似乎无法访问响应的值,它们是JSON。这是我的密码:Javascript 如何使用Promise.allsolited访问fetch/http调用的响应值?,javascript,promise,fetch,Javascript,Promise,Fetch,我正在尝试使用Promise.AllSetted调用3个URL,并在完成所有操作后处理结果。但是,我似乎无法访问响应的值,它们是JSON。这是我的密码: let urlList = ['http...', 'http...', 'http...']; let fetches = urlList.map(url => fetch(url)); Promise.allSettled(fetches) .then((results) => { // (*) re
let urlList = ['http...', 'http...', 'http...'];
let fetches = urlList.map(url => fetch(url));
Promise.allSettled(fetches)
.then((results) => { // (*)
results.forEach((result, num) => {
if (result.status == "fulfilled") {
//result.value is a response object and I cannot seem to access the response.body or the actual text/json value of the response
// in chrome developer tools, I can see the results I want, just can't seem to read them here
console.log(result.value);
}
else if (result.status == "rejected") {
console.log(`Failed Calling: ${urls[num]}\n\treason: ${result.reason}`);
}
});
//call other method with combined results...
})
.catch((err) => {
alert(err);
});
console.log输出:
我错过了什么?提前感谢您的帮助更改此选项:
let fetches = urlList.map(url => fetch(url));
为此:
let fetches = urlList.map(url => fetch(url).then(res => res.json()));
这样,您的Promise.allsolited()
将为您提供一个承诺数组,这些承诺将解析为最终值,而不仅仅是响应头对象,因此您的result.value
属性将是每个承诺的最终值