我遇到的问题是,我要发出许多API请求,由于要返回的数据大小,所有这些请求都需要花费几秒钟的时间才能返回,并且UrlFetchApp.fetchAll(..)仅返回一个空JS对象数组,例如:[{}, {}, {}, ...]。
我的请求数组看起来像这样(为清楚起见而格式化):
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'
}
];
Run Code Online (Sandbox Code Playgroud)
发出我的请求的代码:
responses = UrlFetchApp.fetchAll(requests);
// returns back '[{}, {}]'
console.log(JSON.stringify(responses));
Run Code Online (Sandbox Code Playgroud)
我可以通过数据库确认正在运行API调用,因为AWS RDS性能指标显示数据库查询正在运行,并且我还可以确认API本身通过NewRelic响应为200,这就是我的直觉是m未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 });
Run Code Online (Sandbox Code Playgroud)