【发布时间】:2020-03-08 17:13:10
【问题描述】:
我遇到的问题是我有许多 API 请求我想要发出,所有这些请求都需要几秒钟才能返回,因为要返回的数据的大小,而 UrlFetchApp.fetchAll(..) 只是返回一个数组空 JS 对象,例如:[{}, {}, {}, ...].
我的 requests 数组看起来像这样(为清晰起见格式化):
requests = [
{
validateHttpsCertificates: false,
url: 'https://url.com/api/v2/api_key/endpoint/action?params1=false¶ms2=true'
},
{
validateHttpsCertificates: false,
url: 'https://url.com/api/v2/api_key/endpoint/action?params3=false¶ms4=true'
}
];
提出我的请求的代码:
responses = UrlFetchApp.fetchAll(requests);
// returns back '[{}, {}]'
console.log(JSON.stringify(responses));
我可以通过数据库确认 API 调用正在运行,因为 AWS RDS 性能指标显示数据库查询正在运行,而且我还可以确认 API 本身通过 NewRelic 以 200 响应,这就是为什么我的直觉是我没有正确使用 GAS/UrlFetchApp.fetchAll()。
所以,我想知道:
- GAS 是否同步运行,也就是说,它会等待
.fetchAll()返回,然后再运行console.log(...)行? - 我真的正确调用
fetchAll吗?目前处于亏损状态,Google Appscript 文档充其量只是微薄的。
提前感谢您的帮助。
编辑:
我迁移到fetchAll之后我成功使用fetch,例如:
// synchronously fetching one by one
requests.map(request => UrlFetchAll.fetch(request.url, { validateHttpsCertificates: false });
【问题讨论】:
标签: javascript google-apps-script google-data-studio