【发布时间】:2021-05-23 02:07:26
【问题描述】:
我正在尝试使用 Promise.allSettled 调用 3 个 url 并在全部完成后处理结果。但是,我似乎无法访问响应的值,即 JSON。这是我的代码:
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);
});
我在这里缺少什么?提前感谢您的帮助
【问题讨论】:
标签: javascript promise fetch