javascript - 通过 URL 发布请求的 Asnyc 循环
问题描述
我正在尝试异步遍历一堆 URL,但是我发现虽然这种方法通常可以使用axios.get(url)
它,但当我使用 post 方法尝试它时,它却返回 undefined。
有谁知道为什么会出现这种情况 - 或者在发出帖子请求时有更好的异步循环一堆页面的方法?
const axios = require('axios');
let data = [];
let promises = [];
// 1. Import URLS
const urls = ['https://example.com/',
'https://example.com/page-a.html',
'https://example.com/page-b.html'];
// 2. Queue up the promises
urls.map(function(url) {
promises.push(
axios.post(`https://chromeuxreport.googleapis.com/v1/records:queryRecord?key=${API_KEY}`, {
url: url,
metrics: ["first_contentful_paint",
"first_input_delay",
"largest_contentful_paint",
"cumulative_layout_shift"],
effectiveConnectionType: '4G',
formFactor: 'ALL_FORM_FACTORS'
})
)
});
// 3. Return the responses
axios.all(promises).then(function(results) {
results.map(function(res) {
try {
const { key, metrics } = res.record;
console.log(metrics);
}
catch(e) {
console.error(e);
}
})
});
解决方案
推荐阅读
- reactjs - 反应钩子和使用效果删除我的状态意外结果
- amazon-web-services - 自定义注册表的示例对象路径
- azure - 将多容器组部署到 Azure Serverless
- node.js - 如何传递多个中间件nodejs之一
- amazon-web-services - 即使子网位于不同的可用区,也无法创建 RDS 实例
- blazor - Blazor .net core (3.1) 托管的 webassembly 应用程序从 .Client 应用程序获取 apsettings.json
- c# - 在创建更新场景中处理私有 setter 和 id
- c# - 从 response.Content.ReadAsStringAsync() 中恢复对象
- postgresql - 我可以在 LIMIT 子句中使用百分比而不是数字吗
- tensorflow - 我正在使用 tensorflow 在 colab 中进行编码。我遇到了这个错误。我该如何进行。我尝试安装各种版本的 tf 以及 tf.hub