【问题标题】:axios Promise.all never endsaxios Promise.all 永无止境
【发布时间】:2020-09-24 13:41:20
【问题描述】:
我正在使用带有 axios 的 node.js 来获取一些 url,并且我正在使用 promise.all() 迭代一个 id,但它永远不会结束。我错过了什么吗? listCalls 是包含我想要的所有调用(在 25-70 之间)调用的数组
const result = await Promise.all(listCalls.map(async (call) => await axios.get(call.url)));
console.log(result is, result);
但永远不会显示任何内容。
【问题讨论】:
标签:
node.js
web-scraping
async-await
axios
【解决方案1】:
axios.get 返回一个承诺。这就是您要发送到 Promise.all 的内容:
const result = await Promise.all(listCalls.map(call => axios.get(call.url)))
【解决方案2】:
Promise.all 返回一个包含聚合结果的数组。下面的示例应该使用包含 promise1、promise2、promise3 结果或错误的数组来解析。
Promise.all([promise1, promise2, promise3]).then((values) => {
console.log(values);
}).catch(error => {
console.error(error.message)
});
将您的代码更改为此并检查结果:
const result = await Promise.all(listCalls.map(async (call) => await axios.get(call.url))).then((values) => {
console.log('result is', values);
}).catch(error => {
console.error(error.message)
});